Binary Tree Inorder Traversal leetcode java

题目

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?

题解

中序遍历:递归左 处理当前 递归右。

画图的话就是,之前离散老师教的,从root开始沿着子树画线,遍历完全树,每一个被画线画到2次的就表示遍历到了,依次写出就行了。

递归代码如下:

1     public void helper(TreeNode root, ArrayList<Integer> re){
 2         if(root==null)
 3             return;
 4         helper(root.left,re);
 5         re.add(root.val);
 6         helper(root.right,re);
 7     }
 8     public ArrayList<Integer> inorderTraversal(TreeNode root) {
 9         ArrayList<Integer> re = new ArrayList<Integer>();
10         if(root==null)
11             return re;
12         helper(root,re);
13         return re;
14     }

非递归代码如下:

1 public ArrayList<Integer> inorderTraversal(TreeNode root) {  
 2     ArrayList<Integer> res = new ArrayList<Integer>();  
 3     if(root == null)  
 4         return res;  
 5     LinkedList<TreeNode> stack = new LinkedList<TreeNode>();  
 6     while(root!=null || !stack.isEmpty()){  
 7         if(root!=null){
 8             stack.push(root);
 9             root = root.left; 
10         }else{  
11             root = stack.pop();
12             res.add(root.val); 
13             root = root.right;  
14         }  
15     }  
16     return res;  
17 }

Binary Tree Inorder Traversal leetcode java

时间: 2024-10-25 10:45:52

Binary Tree Inorder Traversal leetcode java的相关文章

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

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 Inorder Traversal ——LeetCode

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? 题目大意:中序遍历一个二叉树,递归的方案太low,用迭代的方式来写? 解题思路:不用递归,那就

Binary Tree Inorder Traversal -- LeetCode 94

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]. Solution 1: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode

Binary Tree Inorder Traversal [leetcode] 非递归的三种解法

第一种方法是Morris Traversal 是O(n)时间复杂度,且不需要额外空间的方法.缺点是需要修改树. 通过将叶子节点的right指向其中序后继. 代码如下 vector<int> inorderTraversal(TreeNode *root) { vector<int> res; TreeNode * cur = root; TreeNode * pre = NULL; while (cur) { if (cur->left == NULL) { res.push

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为了清晰表示

[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? 不使用递归前序遍历,可以

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  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? 分析:求二叉树的中序