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
原题链接
前序遍历:先打印当前节点,再打印当前节点的左子树,最后打印当前节点的右子树 (ABCDEFG)
const preorderTraversal = function(root) { const result = []; function pushRoot(node){ if (node !== null) { result.push(node.val); if (node.left !== null){ pushRoot(node.left); } if (node.right !== null) { pushRoot(node.right); } } } pushRoot(root); return result; };
The text was updated successfully, but these errors were encountered:
利用栈的特性
var preorderTraversal = function(root) { const ret = [] if (!root) return ret const stack = [root] while(stack.length) { const cur = stack.pop() ret.push(cur.val) if (cur.left) stack.push(cur.left) if (cur.right) stack.push(cur.right) } return ret };
二叉树后序遍历同理,中序遍历稍有不同。
Sorry, something went wrong.
No branches or pull requests
原题链接
前序遍历:先打印当前节点,再打印当前节点的左子树,最后打印当前节点的右子树 (ABCDEFG)
The text was updated successfully, but these errors were encountered: