cKK

............当你觉得自己很辛苦,说明你正在走上坡路.............坚持做自己懒得做但是正确的事情,你就能得到别人想得到却得不到的东西............

导航

(Tree)94.Binary Tree Inorder Traversal

Posted on 2016-06-29 11:37  cKK  阅读(158)  评论(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;
    }
}