LeetCode:144 二叉树的前序遍历

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

        }
        return res;
    }
}

 

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