lintcode-easy-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].

Challenge

Can you do it without recursion?

两种做法:

1. 使用递归, 思路比较直接,不多说了

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<Integer>();

        if(root == null)
            return result;

        helper(root, result);

        return result;
    }

    public void helper(TreeNode root, ArrayList<Integer> result){
        if(root == null)
            return;
        else{
            helper(root.left, result);
            result.add(root.val);
            helper(root.right, result);
            return;
        }
    }
}

2. 不使用递归的方法,使用一个stack,维护一个reference p

当p非null时,将p入栈,p指向左子树

当p为null时,栈顶元素出栈,加入arraylist,p再指向右子树

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> result = new ArrayList<Integer>();

        if(root == null)
            return result;

        TreeNode p = root;
        Stack<TreeNode> stack = new Stack<TreeNode>();

        while(p != null || !stack.isEmpty()){
            if(p != null){
                stack.push(p);
                p = p.left;
            }
            else{
                p = stack.pop();
                result.add(p.val);
                p = p.right;
            }
        }

        return result;
    }
}
时间: 2024-12-15 04:12:24

lintcode-easy-Binary Tree Inorder Traversal的相关文章

[lintcode easy]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Challenge Can you do it without recursion? Among preorder,inorder and postorder binary tree

[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,其

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) //非递归的中序遍历(