美文网首页
后序遍历

后序遍历

作者: crazydane | 来源:发表于2017-06-06 01:48 被阅读0次

反转前序遍历

public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
        if (root != null) stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode curr = stack.pop();
            result.add(curr.val);
            if (curr.left != null) stack.push(curr.left);
            if (curr.right != null) stack.push(curr.right);
        }

        Collections.reverse(result);
        return result;
    }
}

相关文章

网友评论

      本文标题:后序遍历

      本文链接:https://www.haomeiwen.com/subject/vttgfxtx.html