LeetCode-Binary Tree Inorder Traversal

来源:互联网 发布:肿泡眼双眼皮手术知乎 编辑:程序博客网 时间:2024/06/04 23:45
作者:disappearedgod
文章出处:http://blog.csdn.net/disappearedgod/article/details/38899153
时间:2014-8-30

题目

.

Binary Tree Inorder Traversal

 Total Accepted: 27734 Total Submissions: 78000My 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 TimeStatusRun TimeLanguage2 minutes agoAccepted376 msjava

返回

 LeetCode Solution(持续更新,java>c++)

0 0
原创粉丝点击