LeetCode:94 二叉树的中序遍历

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        Deque<TreeNode> stack  = new LinkedList<>();
        TreeNode t = root;
        List<Integer> res = new LinkedList<>();
        while(t!=null||!stack.isEmpty()){
            if(t!=null){
                stack.push(t);
                t = t.left;
            }
            else{
                t = stack.pop();
                res.add(t.val);
                t = t.right;
            }
        }
        return res;
    }
}

 

posted @ 2020-10-06 08:15  dlooooo  阅读(87)  评论(0编辑  收藏  举报