• lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历


    题目:

    二叉树的后序遍历

    给出一棵二叉树,返回其节点值的后序遍历。

    样例

    给出一棵二叉树 {1,#,2,3},

       1
        
         2
        /
       3

    返回 [3,2,1]

    挑战

    你能使用非递归实现么?

    解题:

    递归程序好简单

    Java程序:

    /**
     * Definition of TreeNode:
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left, right;
     *     public TreeNode(int val) {
     *         this.val = val;
     *         this.left = this.right = null;
     *     }
     * }
     */
    public class Solution {
        /**
         * @param root: The root of binary tree.
         * @return: Postorder in ArrayList which contains node values.
         */
        public ArrayList<Integer> postorderTraversal(TreeNode root) {
            // write your code here
            ArrayList<Integer> res = new ArrayList<Integer>();
            res = postorder(res,root);
            return res;
        }
        public ArrayList<Integer> postorder(ArrayList<Integer> res,TreeNode root){
            if(root==null)
                return res;
            if(root.left!=null)
                res = postorder(res,root.left);
            if(root.right!=null)
                res = postorder(res,root.right);
            res.add(root.val);
            return res;
        }
    }
    View Code

    总耗时: 1210 ms

    Python程序:

    """
    Definition of TreeNode:
    class TreeNode:
        def __init__(self, val):
            self.val = val
            self.left, self.right = None, None
    """
    
    
    class Solution:
        """
        @param root: The root of binary tree.
        @return: Postorder in ArrayList which contains node values.
        """
        def postorderTraversal(self, root):
            # write your code here
            res = []
            res = self.postorder(res,root)
            return res
        
        def postorder(self,res,root):
            if root==None:
                return res
            if root.left!=None:
                res = self.postorder(res,root.left)
            if root.right!=None:
                res = self.postorder(res,root.right)
            res.append(root.val)
            return res
    View Code

    总耗时: 380 ms

    非递归程序,直接来源

    Java程序:

    /**
     * Definition of TreeNode:
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left, right;
     *     public TreeNode(int val) {
     *         this.val = val;
     *         this.left = this.right = null;
     *     }
     * }
     */
    public class Solution {
        /**
         * @param root: The root of binary tree.
         * @return: Postorder in ArrayList which contains node values.
         */
        public ArrayList<Integer> postorderTraversal(TreeNode root) {
            // write your code here
            int a = 1;  
            ArrayList<TreeNode> s = new ArrayList<TreeNode>();  
            ArrayList<Integer> res = new ArrayList<Integer>();  
            if (root == null) return res;  
            while(a == 1){  
                while(root.left != null || root.right != null){  
                    if (root.left != null){  
                        s.add(root);  
                        root = root.left;  
                    }  
                    else{  
                        s.add(root);  
                        root = root.right;  
                    }  
                }  
                TreeNode y = s.get(s.size()-1);  
                while (root == y.right || y.right == null){  
                    res.add(root.val);  
                    s.remove(s.size()-1);  
                    if (s.size() == 0){  
                        a = 0;  
                        res.add(y.val);  
                        break;  
                    }  
                    root = y;  
                    y = s.get(s.size()-1);  
                }  
                if (root == y.left && y.right != null){  
                    res.add(root.val);  
                    root = y.right;  
                }  
            }  
            return res; 
        }
    }
    View Code

    总耗时: 1388 ms

    Python程序:

    """
    Definition of TreeNode:
    class TreeNode:
        def __init__(self, val):
            self.val = val
            self.left, self.right = None, None
    """
    
    
    class Solution:
        """
        @param root: The root of binary tree.
        @return: Postorder in ArrayList which contains node values.
        """
        def postorderTraversal(self, root):
            # write your code here
            a = 1  
            s = [root]  
            res = []  
            if root is None:  
                return res[1:1]  
            while a == 1:  
                while root.left is not None or root.right is not None:  
                    if root.left is not None:  
                        s.append(root)  
                        root = root.left  
                    else:  
                        s.append(root)  
                        root = root.right  
                y = s[len(s)-1]  
                while root == y.right or y.right is None:  
                    res.append(root.val)  
                    del s[len(s)-1]  
                    if len(s) == 1:  
                        a = 0  
                        res.append(y.val)  
                        break  
                    root = y  
                    y = s[len(s)-1]  
                if root == y.left and y.right is not None:  
                    res.append(root.val)  
                    root = y.right  
            return res 
    View Code

    总耗时: 360 ms

  • 相关阅读:
    华为精益敏捷专家:DevOps转型中的那些坑
    极致进化-敏捷进化型企业的未来畅想
    DevOps的工程化
    京东精益敏捷教练分享:敏捷助力产品创新!
    敏捷开发进度管理之燃尽图
    手把手教你进行Scrapy中item类的实例化操作
    手把手教你使用ADB卸载手机内置App软件
    手把手教你使用Python生成图灵智能小伙伴,实现工作助手/闲聊功能
    手把手教你利用Pyecharts库对IP代理数据进行数据可视化分析
    手把手教你使用Python爬取西刺代理数据(下篇)
  • 原文地址:https://www.cnblogs.com/bbbblog/p/4866348.html
Copyright © 2020-2023  润新知