[LeetCode]Binary Tree Postorder Traversal,解题报告

原创 2013年12月02日 14:49:46

题目

Given a binary tree, return the postorder traversal of its nodes' values.

Note: Recursive solution is trivial, could you do it iteratively?

思路

题目给的Note提示,意思是用非递归实现二叉树的后序遍历

之前用c很详细的描述过二叉树各种递归、非递归遍历,想了解原理的同学移步:http://blog.csdn.net/wzy_1988/article/details/8450952


AC代码

import java.util.ArrayList;
import java.util.LinkedList;

public class BinaryTreePostorderTraversal {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int x) {
            this.val = x;
        }
    }

    public static ArrayList<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
        TreeNode pre = null;
        
        while (!stack.isEmpty() || root != null) {
            if (root != null) {
                stack.addFirst(root);
                root = root.left;
            } else {
                root = stack.removeFirst();
                if (root.right == null || root.right == pre) {
                    pre = root;
                    list.add(root.val);
                    root = null;
                } else {
                    stack.addFirst(root);
                    root = root.right;
                }
            }
        }
        
        return list;
    }
}


后记

二叉树的非递归后序遍历应该算是遍历中还稍微有点难度的,这个看懂了相信非递归前序、中序遍历都不是问题,加油
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[leetcode] 145. Binary Tree Postorder Traversal 解题报告

题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return th...

leetCode解题报告之Binary Tree Postorder Traversal

题目: Binary Tree Postorder Traversal   Given a binary tree, return the postorder travers...

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...

[Leetcode] 103. Binary Tree Zigzag Level Order Traversal 解题报告

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, fro...

剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

剑指offer 重建二叉树 参与人数:5246  时间限制:1秒  空间限制:32768K 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍...

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...

LeetCode—Binary Tree Preorder Traversal 解题报告

题目解析: 这道题目是“二叉树的前序遍历”。通常有两种做法,递归求解以及循环求解。 递归求解,较为简单,先访问当前节点、访问左孩子结点、在访问右孩子节点。详细可参照如下代码。 ...

Leetcode 103. Binary Tree Zigzag Level Order Traversal 二叉树Zigzag遍历 解题报告

1 解题思想首先这道题和 Leetcode 102. Binary Tree Level Order Traversal 二叉树按层遍历 解题报告 很接近,Zigzag同样是按层遍历,但是区别是对...
  • MebiuW
  • MebiuW
  • 2016年09月09日 12:26
  • 776

[leetcode]94. Binary Tree Inorder Traversal@Java解题报告

https://leetcode.com/problems/binary-tree-inorder-traversal/description/ Given a ...

【LeetCode】Binary Tree Level Order Traversal II 解题报告

Binary Tree Level Order Traversal II [LeetCode] Given a binary tree, return the bottom-up level orde...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode]Binary Tree Postorder Traversal,解题报告
举报原因:
原因补充:

(最多只允许输入30个字)