Open ChuChencheng opened 4 years ago
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var preorderTraversal = function(root) {
const result = []
if (!root) return result
const stack = [root]
while (stack.length) {
const node = stack.pop()
if (node.right) stack.push(node.right)
if (node.left) stack.push(node.left)
result.push(node.val)
}
return result
};
问题
LeetCode 144
解
递归
非递归