We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LeetCode 144
/** * 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 = [] const traverse = (node) => { if (!node) return result.push(node.val) if (node.left) traverse(node.left) if (node.right) traverse(node.right) } traverse(root) return result };
/** * 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 = [] const stack = [] let node = root while (node || stack.length) { while (node) { result.push(node.val) stack.push(node) node = node.left } node = stack.pop().right } return result };
The text was updated successfully, but these errors were encountered:
/** * 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 };
Sorry, something went wrong.
No branches or pull requests
问题
LeetCode 144
解
递归
非递归
The text was updated successfully, but these errors were encountered: