Pre-order
preorder(node) if node == null then return visit(node) preorder(node.left) preorder(node.right) |
iterativePreorder(node) parentStack = empty stack while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) visit(node) if(node.right ≠ null) parentStack.push(node.right) node = node.left else node = parentStack.pop() |
In-order
inorder(node) if node == null then return inorder(node.left) visit(node) inorder(node.right) |
iterativeInorder(node) parentStack = empty stack while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) parentStack.push(node) node = node.left else node = parentStack.pop() visit(node) node = node.right |
Post-order
postorder(node) if node == null then return postorder(node.left) postorder(node.right) visit(node) |
iterativePostorder(node) parentStack = empty stack lastnodevisited = null while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) parentStack.push(node) node = node.left else peeknode = parentStack.peek() if (peeknode.right ≠ null and lastnodevisited ≠ peeknode.right) /* if right child exists AND traversing node from left child, move right */ node = peeknode.right else visit(peeknode) lastnodevisited = parentStack.pop()
|
Pre-order:
public List<Integer> preorderTraversal(TreeNode node) { List<Integer> list = new LinkedList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while(!stack.isEmpty() || node != null) { if(node != null) { list.add(node.val); if(node.right != null) stack.push(node.right); node = node.left; } else { node = stack.pop(); } } return list; }
In-order:
private List<Integer> inorderIterative(TreeNode node) { List<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while(!stack.isEmpty() || node != null) { if(node != null) { stack.push(node); node = node.left; } else { TreeNode p = stack.pop(); list.add(p.val); node = p.right; } } return list; }
Posr-order:
private List<Integer> postorderIterative(TreeNode node) { List<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode last = null; while(!stack.empty() || node != null) { if(node != null) { stack.push(node); node = node.left; } else { TreeNode p = stack.peek(); if(p.right != null && p.right != last) { node = p.right; } else { list.add(p.val); last = stack.pop(); } } } return list; }
Reference:
相关推荐
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
javascript js_leetcode题解之145-binary-tree-postorder-traversal.js
javascript js_leetcode题解之144-binary-tree-preorder-traversal.js
js js_leetcode题解之102-binary-tree-level-order-traversal.js
js js_leetcode题解之107-binary-tree-level-order-traversal-ii.js
js js_leetcode题解之103-binary-tree-zigzag-level-order-traversal.js
javascript js_leetcode题解之94-binary-tree-inorder-traversal.js
python python_leetcode题解之145_Binary_Tree_Postorder_Traversal
python python_leetcode题解之144_Binary_Tree_Preorder_Traversal
python python_leetcode题解之102_Binary_Tree_Level_Order_Traversal
python python_leetcode题解之107_Binary_Tree_Level_Order_Traversal_II
python python_leetcode题解之103_Binary_Tree_Zigzag_Level_Order_Traversal
python python_leetcode题解之094_Binary_Tree_Inorder_Traversal
c语言基础 c语言_leetcode题解之0094_binary_tree_inorder_traversal.zip
94.binary-tree-inorder-traversal (二叉树的中序遍历) 101.symmetric-tree (对称二叉树) 102.binary-tree-level-order-traversal (二叉树的层序遍历) 104.maximum-depth-of-binary-tree (二叉树的最大深度) 105....
4. leetcode-145-Binary-Tree-Postorder-Traversal.md:第145题,二叉树的后序遍历,是关于树结构和递归的问题。 5. leetCode-5-Longest-Palindromic-Substring.md:第5题,最长回文子串,考察字符串处理和动态规划...
144-Binary Tree Preorder Traversal94-Binary Tree Inorder Traversal145-Binary Tree Postorder Traversal(后续的非递归时间不够可以先跳过,有点难)层次遍历,队列辅助,相当于广搜。102-Binary Tree Level ...
5. **递归与函数**:递归是解决许多算法问题的有效手段,如"Binary Tree Level Order Traversal"使用层次遍历(广度优先搜索)求解。而"Factorial Trailing Zeroes"则通过递归计算阶乘尾部零的个数。 6. **复杂度...
binary-tree-postorder-traversal 树 binary-tree-preorder-traversal 链表 linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-...