LintCode -- 二叉树的后序遍历

LintCode -- binary-tree-postorder-traversal(二叉树的后序遍历)

原题链接:http://www.lintcode.com/zh-cn/problem/binary-tree-postorder-traversal/


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

样例

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

   1
    \
     2
    /
   3

返回 [3,2,1]

挑战

你能使用非递归实现么?


非递归实现(C++、Java、Python):

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in vector which contains node values.
     */
public:
    vector<int> postorderTraversal(TreeNode *root) {
        // write your code here
        int a = 1;
        vector<TreeNode *> s;
        vector<int> res;
        if (root == NULL) return res;
        while(a == 1){
            while(root->left != NULL || root->right != NULL){
                if(root->left != NULL){
                    s.push_back(root);
                    root = root->left;
                }
                else{
                    s.push_back(root);
                    root = root->right;
                }
            }
            TreeNode *y = s.back();
            while (root == y->right || y->right == NULL){
                res.push_back(root->val);
                s.pop_back();
                if (s.size() == 0){
                    a = 0;
                    res.push_back(y->val);
                    break;
                }
                root = y;
                y = s.back();
            }
            if (root == y->left && y->right != NULL) {
                res.push_back(root->val); 
                root = y->right;
            }
        }
        return res;
    }
};
/**
 * 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;
    }
}

"""
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 = [0]
        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[1:]



你可能感兴趣的:(算法,二叉树,遍历,lintcode)