42: Binary Tree Postorder Traversal

/************************************************************************/
            /*       42:  Binary Tree Postorder Traversal                               */
            /************************************************************************/
            /*
             * Given a binary tree, return the postorder traversal of its nodes‘ values.

For example:
Given binary tree {1,#,2,3},

1
    \
     2
    /
   3

return [3,2,1].
             * */
            
            /****后序遍历 PostOrder tree  递归复杂度****Time: O(n), Space: O(n).****************************************************************/
            /*
             * 应用1:
             * 计算文件夹所占的磁盘容量时,先要计算根节点下的所有文件夹的大小,然后层层网上
             *
             * TODO
             * */

  public List<Integer> postorderTraversal(TreeNode root)
            {
                List<Integer> source=new ArrayList<Integer>();
                postorderTree(root,source);
                return source;
            }

            private void postorderTree(TreeNode node,List<Integer> nodes)
            {
                    if(node==null)
                    {
                        return;
                    }
                    System.out.println("pre root-->"+node.val);
                    postorderTree(node.left,nodes);
                    postorderTree(node.right,nodes);
                    nodes.add(node.val);
                    System.out.println("-->"+node.val);
            } 

            /****后序遍历 PostOrder tree  Time: O(n), Space: O(n) 栈迭代方法实现的********************************************************************/
            //实现的有点繁琐好像,有没有其他办法呢? 改进版本见 :postorderTraversal3
            public List<Integer>postorderTraversal2(TreeNode root)
            {
                List<Integer> results=new ArrayList<Integer>();
                Stack<Map<TreeNode, Boolean> > stack=new Stack<Map<TreeNode, Boolean> >();
                TreeNode node=null;
                node=root;
                while(node!=null||!stack.isEmpty())
                {
                    if(node!=null)
                    {
                        Map<TreeNode, Boolean> temp=new HashMap<TreeNode, Boolean>();
                        temp.put(node, false);
                        stack.push(temp);
                        node=node.left;
                    }
                    else
                    {
                        Map<TreeNode, Boolean> tempnode= stack.pop();
                        for (Map.Entry<TreeNode, Boolean> entry :tempnode.entrySet())
                        {
                            if(entry.getKey().left==null&&entry.getKey().right==null&&entry.getValue()==false) // 弹出叶子节点,按照左节点到右节点的弹出顺序
                            {
                                results.add(entry.getKey().val);
                                //System.out.println(entry.getKey().val);
                                node=entry.getKey();
                                node=node.right;
                            }
                            else if(entry.getKey().right!=null&&entry.getValue()==false)// 中间路径上的根节点第一遍时不弹出,设置已访问过
                            {
                                entry.setValue(true);
                                stack.push(tempnode);
                                node=entry.getKey();
                                node=node.right;
                            }
                        else   // 中间路径上的根节点第二遍时弹出
                            {
                                results.add(entry.getKey().val);
                                //System.out.println(entry.getKey().val);
                                node=null;
                            }
                        }
                    }
                }
                return results;

            }

            /****后序遍历 PostOrder tree 简洁版本1  Time: O(n), Space: O(n) 栈迭代方法实现的********************************************************************/
            /*
             *
             * 算法思路: 对应函数:  postorderTraversal3,postorderTraversal4
             *
             * pre-order traversal is root-left-right, and post order is left-right-root.
             * modify the code for pre-order to make it root-right-left,
             * and then reverse the output so that we can get left-right-root .

    Create an empty stack, Push root node to the stack.

    Do following while stack is not empty.

    2.1. pop an item from the stack and print it.

    2.2. push the left child of popped item to stack.

    2.3. push the right child of popped item to stack.

    reverse the ouput.

             * */
            public List<Integer>postorderTraversal3(TreeNode root)
            {
                List<Integer> results=new ArrayList<Integer>();
                Stack<TreeNode> stack=new Stack<TreeNode>();
                TreeNode node=null;
                node=root;
                while(node!=null||!stack.isEmpty())
                {
                    if(node!=null)
                    {
                        //System.out.println("-->"+node.val);
                        results.add(node.val);
                        stack.push(node);
                        node=node.right;
                    }
                    else
                    {
                        node= stack.pop();
                        node=node.left;
                    }
                }
                Collections.reverse(results);
                return results;
            }

            public List<Integer>postorderTraversal4(TreeNode root)
            {
                List<Integer> results=new ArrayList<Integer>();
                Stack<TreeNode> stack=new Stack<TreeNode>();
                stack.push(root);
                TreeNode node=null;
                while(!stack.isEmpty())
                {
                    node=stack.pop();
                    results.add(node.val);
                    if(node.left!=null)
                    {
                        stack.push(node.left);
                    }
                    if(node.right!=null)
                    {
                        stack.push(node.right);
                    }
                }
                Collections.reverse(results);
                return results;
            }
时间: 2024-11-07 10:52:45

42: Binary Tree Postorder Traversal的相关文章

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

LeetCode: Binary Tree Postorder Traversal 解题报告

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

[LeetCode][JavaScript]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 同样的配方,同样的味道.

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. c++版: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could y