post Order Tree traverse - finally AC

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:  
    vector<int> postorderTraversal(TreeNode *root) {  
        vector<int> result;  
        if(root==NULL) return result;
        TreeNode *pre= root;
  stack<TreeNode*> s;
  s.push(root);
  TreeNode *prev = NULL;
  while (!s.empty()) {
    TreeNode *curr = s.top();
    if (!prev || prev->left == curr || prev->right == curr) {
      if (curr->left)// try to visit the left node;
        s.push(curr->left);
      else if (curr->right)// if left node is empty ; push the right child
        s.push(curr->right);
    } else if (curr->left == prev) {
      if (curr->right)
        s.push(curr->right);
    } else {
      //cout << curr->data << " ";
      result.push_back(curr->val);
      s.pop();
    }
    prev = curr;
  }
    }
};


Some code can push your code to the end; so do not forget them; 

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:  
    vector<int> postorderTraversal(TreeNode *root) {  
        vector<int> result;  
        if(root==NULL) return result;
        TreeNode *pre= NULL;
        stack<TreeNode *> stk;
        stk.push(root);
        while(!stk.empty())
        {
            TreeNode *cur = stk.top();
            if(pre==NULL || pre->left == cur || pre->right == cur)
            {// if the pre is the father of cur;means the whole visit has just done;we should go down to the deep of the tree;
                if(cur->left)
                {
                    stk.push(cur->left);
                }
                else
                {// if cur do not have left child;then
                    if(cur->right)
                    {
                        stk.push(cur->right);
                    }
                }
            }
            else
            if(cur->left == pre)//pre is the child of cur; 1 pre is the right child of cur;2 pre is the left child of cur
            {// one's left child is vistied then the right child should be visted; => so we try to visit its right child
                if(cur->right)
                {
                    stk.push(cur->right);
                }
            }
            else
            {// cur->right == pre; then we visit cur; because one's right child is visited then we should visit this node
                result.push_back(cur->val);
                stk.pop();
            }
            pre = cur; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }
    }
};

http://leetcode.com/2010/10/binary-tree-post-order-traversal.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值