[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal:

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?

不使用递归前序遍历,可以借助栈的帮助实现。对于一个特定节点,前序遍历顺序为: 根,左,右。所以入栈顺序为 右,左,根。由于根节点的遍历和展开(研究其右节点和左节点)是同时的,所以根节点出栈的同时即可加入遍历结果中,然后研究其右节点和左节点,如果不为空即可入栈。

 1  public List<Integer> preorderTraversal(TreeNode root) {
 2         List<Integer> ls = new ArrayList<Integer>();
 3         if(root==null)
 4             return ls;
 5         Stack<TreeNode> st = new Stack<TreeNode>();
 6         st.push(root);
 7
 8         while(!st.isEmpty())
 9         {
10             TreeNode temp = st.pop();
11             ls.add(temp.val);
12             if(temp.right!=null)
13                 st.push(temp.right);
14             if(temp.left!=null)
15                 st.push(temp.left);
16         }
17         return ls;
18     }

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来判断是否已经访问过。

压栈顺序为 右, 根, 左(因为中序遍历顺序为左 根 右)

 1 public List<Integer> inorderTraversal(TreeNode root) {
 2         List<Integer> ls = new ArrayList<Integer>();
 3         if(root==null)
 4             return ls;
 5         Stack<TreeNode> st = new Stack<TreeNode>();
 6         HashSet<TreeNode> hs = new HashSet<TreeNode>();
 7
 8         st.push(root);
 9         while(!st.isEmpty())
10         {
11             TreeNode temp = st.pop();
12             if(hs.contains(temp))
13             {
14                 ls.add(temp.val);
15                 continue;
16             }
17             hs.add(temp);
18             if(temp.right!=null)
19                 st.push(temp.right);
20             st.push(temp);
21             if(temp.left!=null)
22                 st.push(temp.left);
23         }
24         return ls;
25     }

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?

与中序遍历一样,只不过压栈顺序为根,右,左(后序遍历顺序为左,右,根)

 1 public List<Integer> postorderTraversal(TreeNode root) {
 2         List<Integer> ls = new ArrayList<Integer>();
 3         if(root==null)
 4             return ls;
 5         Stack<TreeNode> st = new Stack<TreeNode>();
 6         HashSet<TreeNode> hs = new HashSet<TreeNode>();
 7
 8         st.push(root);
 9         while(!st.isEmpty())
10         {
11             TreeNode temp = st.pop();
12             if(hs.contains(temp))
13             {
14                 ls.add(temp.val);
15                 continue;
16             }
17             hs.add(temp);
18             st.push(temp);
19             if(temp.right!=null)
20                 st.push(temp.right);
21             if(temp.left!=null)
22                 st.push(temp.left);
23         }
24         return ls;
25     }
时间: 2024-10-05 04:55:14

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal的相关文章

Construct Binary Tree from Preorder and Inorder Traversal leetcode java

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 题解: 1 / \ 2 3 / \ / \ 4 5 6 7 对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 1 2 4 5 3 6 7 中序遍历为: 4 2 5 1 6 3 7为了清晰表示

Binary Tree Preorder Traversal leetcode java

题目: 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? 题解: 递归做法如下: 1     public void helper(Tree

Binary Tree Postorder Traversal leetcode java

题目: 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? 题解: 递归方法代码: 1     public void helper(Tre

[LeetCode][JavaScript]Binary Tree Preorder Traversal

Binary Tree Preorder Traversal 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? https://leetcod

LeetCode: Binary Tree Preorder Traversal 解题报告

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

[LeetCode 题解]: Binary Tree Preorder Traversal

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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 题解: 这道题跟pre+in一样的方法做,只不过找左子树右子树的位置不同而已. 1 / \ 2 3 / \ / \ 4 5 6 7 对于上图的树来说, index: 0 1 2 3 4 5 6 中序遍历为: 4 2

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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? [题意] 非递归返回先序遍历结果 [思路] 维护一个栈即可 [代码] /** *