[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal
2014-10-04 01:46
381 查看
Binary Tree PreOrder Traversal:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
不使用递归前序遍历,可以借助栈的帮助实现。对于一个特定节点,前序遍历顺序为: 根,左,右。所以入栈顺序为 右,左,根。由于根节点的遍历和展开(研究其右节点和左节点)是同时的,所以根节点出栈的同时即可加入遍历结果中,然后研究其右节点和左节点,如果不为空即可入栈。
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
中序遍历比前序遍历复杂一些,主要是需要区分得到的节点是需要展开还是直接遍历。一般来说第一次访问节点则展开,并且自己重新入栈,第二次从栈中访问到则计入遍历。这里采用HashSet来判断是否已经访问过。
压栈顺序为 右, 根, 左(因为中序遍历顺序为左 根 右)
Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
与中序遍历一样,只不过压栈顺序为根,右,左(后序遍历顺序为左,右,根)
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree
{1,#,2,3},
1 \ 2 / 3
return
[1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?
不使用递归前序遍历,可以借助栈的帮助实现。对于一个特定节点,前序遍历顺序为: 根,左,右。所以入栈顺序为 右,左,根。由于根节点的遍历和展开(研究其右节点和左节点)是同时的,所以根节点出栈的同时即可加入遍历结果中,然后研究其右节点和左节点,如果不为空即可入栈。
public List<Integer> preorderTraversal(TreeNode root) { List<Integer> ls = new ArrayList<Integer>(); if(root==null) return ls; Stack<TreeNode> st = new Stack<TreeNode>(); st.push(root); while(!st.isEmpty()) { TreeNode temp = st.pop(); ls.add(temp.val); if(temp.right!=null) st.push(temp.right); if(temp.left!=null) st.push(temp.left); } return ls; }
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree
{1,#,2,3},
1 \ 2 / 3
return
[1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
中序遍历比前序遍历复杂一些,主要是需要区分得到的节点是需要展开还是直接遍历。一般来说第一次访问节点则展开,并且自己重新入栈,第二次从栈中访问到则计入遍历。这里采用HashSet来判断是否已经访问过。
压栈顺序为 右, 根, 左(因为中序遍历顺序为左 根 右)
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> ls = new ArrayList<Integer>(); if(root==null) return ls; Stack<TreeNode> st = new Stack<TreeNode>(); HashSet<TreeNode> hs = new HashSet<TreeNode>(); st.push(root); while(!st.isEmpty()) { TreeNode temp = st.pop(); if(hs.contains(temp)) { ls.add(temp.val); continue; } hs.add(temp); if(temp.right!=null) st.push(temp.right); st.push(temp); if(temp.left!=null) st.push(temp.left); } return ls; }
Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
{1,#,2,3},
1 \ 2 / 3
return
[3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
与中序遍历一样,只不过压栈顺序为根,右,左(后序遍历顺序为左,右,根)
public List<Integer> postorderTraversal(TreeNode root) { List<Integer> ls = new ArrayList<Integer>(); if(root==null) return ls; Stack<TreeNode> st = new Stack<TreeNode>(); HashSet<TreeNode> hs = new HashSet<TreeNode>(); st.push(root); while(!st.isEmpty()) { TreeNode temp = st.pop(); if(hs.contains(temp)) { ls.add(temp.val); continue; } hs.add(temp); st.push(temp); if(temp.right!=null) st.push(temp.right); if(temp.left!=null) st.push(temp.left); } return ls; }
相关文章推荐
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java
- leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal 分类: leetCode 2015-06-22 08:26 177人阅读 评论(0) 收藏
- [JAVA]LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal leetcode java
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- leetcode - Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode][Java] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal
- 【Leetcode】Binary Tree Preorder Traversal in JAVA
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode-105]Construct Binary Tree from Preorder and Inorder Traversal(java)
- 【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】
- [Leetcode] Construct Binary Tree from Inorder and Postorder/Preorder and Inorder Traversal
- [JAVA]LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal
- leetcode - Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal
- 【LeetCode】Binary Tree Preorder &Inorder & Postorder Traversal
- LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal
- 【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
- leetcode JAVA Construct Binary Tree from Preorder and Inorder Traversal 难度系数3 3.37