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:]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值