首页 > 其他 > 详细

(Tree)94.Binary Tree Inorder Traversal

时间:2016-06-29 12:51:04      阅读:135      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for a binary tree node.
 * 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> res=new ArrayList();
        ArrayDeque<TreeNode> stack=new ArrayDeque();  //双端队列的使用
        TreeNode tmp=root;
        while(!stack.isEmpty() || tmp!=null){       //双判断
            if(tmp!=null){
                stack.push(tmp);
                tmp=tmp.left;
            }
            else{
                tmp=stack.pop();
                res.add(tmp.val);
                tmp=tmp.right;
            }
        }
        return res;
    }
}

  

(Tree)94.Binary Tree Inorder Traversal

原文:http://www.cnblogs.com/kydnn/p/5626282.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!