[lintcode] Binary Tree Inorder Traversal

Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes‘ values.

Example

Given binary tree {1,#,2,3},

   1
         2
    /
   3

return [1,3,2].

SOLUTION 1:

递归方法,具体代码可以参照preorder traversal,这里就不赘述了。

SOLUTION 2:

分治法,Divide & conquer,其实这个代码也可以参照preorder traversal,只贴个代码吧:

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        if (root == null){
            return result;
        }
        ArrayList<Integer> left = inorderTraversal(root.left);
        ArrayList<Integer> right = inorderTraversal(root.right);
        // 差别就是result里面先加入了left,再root,再right
        result.addAll(left);
        result.add(root.val);
        result.addAll(right);
        return result;
    }
}

  

SOLUTION 3:

非递归,non-recursion,首先我们模拟一下这个中序遍历过程,就是用一个cur指针从root 一路走到最左下角叶节点(DFS),然后逐层回退,访问回退节点的右节点,当走到右节点的叶节点时再继续回退找下一个右节点。

由于stack.pop()之后就找不到这个树上的点的位置了,所以要用一个点纪录一下,这个根节点,然后继续访问以这个记录点为根的右节点。代码如下:

public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        if (root == null){
            return result;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode cur = root;
        while (cur != null || !stack.empty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.peek();
            stack.pop();
            result.add(cur.val);
            cur = cur.right;
        }
        return result;
    }
}

  

时间: 2024-10-23 02:22:15

[lintcode] Binary Tree Inorder Traversal的相关文章

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

41: Binary Tree Inorder Traversal

/************************************************************************/            /*       41:  Binary Tree Inorder Traversal                               */            /************************************************************************/  

Binary Tree Inorder Traversal

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? 先贴递归实现的吧 1 /** 2

LeetCode: Binary Tree Inorder Traversal 解题报告

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    /   3return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? co

[LeetCode][JavaScript]Binary Tree Inorder Traversal

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? https://leetcode.

【LeetCode】Binary Tree Inorder Traversal (2 solutions)

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? 解法一:递归 /** * Defi

LeetCode:Binary Tree Inorder Traversal

题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的中序遍历(

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