LeetCode-Binary Tree Inorder Traversal

34 篇文章 0 订阅
作者:disappearedgod
时间:2014-8-30

题目

.

Binary Tree Inorder Traversal

  Total Accepted: 27734  Total Submissions: 78000 My Submissions

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?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

解法

错误算法

这里面递归算法不能过。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<Integer>();
        return visit(root, list);
    }
    public List<Integer> visit(TreeNode root, List<Integer> list){
		list.addAll(visit(root.left, list));
        if(root == null)
            return list;
        if(root.right == null && root.left == null)
            list.add(root.val);
        list.addAll(visit(root.right, list));
        return list;
    }
}



/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<Integer>();
        String res = visit(root, new String());
        if(res == null)
            return list;
        String[] ele = res.split(",");
        for(String s : ele){
           if(s.length() != 0)
            list.add(Integer.valueOf(s));
        }
        return list;
    }
    public String visit(TreeNode root, String res){
		res += visit(root.left, res);
        if(root == null)
            return res;
        if(root.right == null && root.left == null)
            res += "," + root.val;
        res += visit(root.right, res);
        return res;
    }
}

非递归算法

非递归算法需要1个指针,1个栈。
中序遍历是,(left)先压栈,(right)里面先list.add ,然后找右边。


/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        Stack<TreeNode> s = new Stack<TreeNode>();
        TreeNode p = root;
        while(!s.isEmpty() || p != null){
            while(p != null){
                //preOrder list.add(p.val);
                s.push(p);
                p = p.left;
            }
            if(!s.isEmpty()){
                p = s.peek();
                list.add(p.val);
                s.pop();
                p = p.right;
            }
        }
        return list;
    }
}



结果

递归算法

Submission Result: Time Limit Exceeded
Last executed input: {-64,12,18,-4,-53,#,76,#,-51,#,#,-93,3,#,-31,47,#,3,53,-81,33,4,#,-51,-44,-60,11,#,#,#,#,78,#,-35,-64,26,-81,-31,27,60,74,#,#,8,-38,47,12,-24,#,-59,-49,-11,-51,67,#,#,#,#,#,#,#,-67,#,-37,-19,10,-55,72,#,#,#,-70,17,-4,#,#,#,#,#,#,#,3,80,44,-88,-91,#,48,-90,-30,#,#,90,-34,37,#,#,73,-38,-31,-85,-31,-96,#,#,-18,67,34,72,#,-17,-77,#,56,-65,-88,-53,#,#,#,-33,86,#,81,-42,#,#,98,-40,70,-26,24,#,#,#,#,92,72,-27,#,#,#,#,#,#,-67,#,#,#,#,#,#,#,-54,-66,-36,#,-72,#,#,43,#,#,#,-92,-1,-98,#,#,#,#,#,#,#,39,-84,#,#,#,#,#,#,#,#,#,#,#,#,#,-93,#,#,#,98}

非递归算法

Submit Time Status Run Time Language
2 minutes ago Accepted 376 ms java

返回


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值