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

原文地址:https://www.cnblogs.com/bbbblog/p/4866348.html