码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】Binary Tree Postorder Traversal

时间:2014-08-20 13:58:32      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   os   io   strong   for   ar   

Given a binary tree, return the postorder traversal of its nodes‘ values.

For example:
Given binary tree {1,#,2,3},

   1
         2
    /
   3

 

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

 

1st ( 2 tries)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
//help from http://www.cnblogs.com/dolphin0520/archive/2011/08/25/2153720.html
//there is all kind all traversal methods with binary tree in this 
class Solution 
{
public:
    vector<int> postorderTraversal(TreeNode *root) 
    {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        TreeNode *iter = NULL;
        TreeNode *preiter = NULL;
        vector<int> re;
        stack<TreeNode*> st;
        if(root == NULL)
            return re;
        st.push(root);
        while(!st.empty())
        {
            iter = st.top();
            if(iter->left == NULL&&iter->right == NULL)
            {
                re.push_back(iter->val);
                preiter = iter;
                st.pop();
            }
            else if(preiter != NULL && (preiter == iter->left || preiter == iter->right))
            {
                re.push_back(iter->val);
                preiter = iter;
                st.pop();
            }
            else
            {
                if(iter->right != NULL)
                    st.push(iter->right);
                if(iter->left != NULL)
                    st.push(iter->left);
            }
        }
        return re;
    }
};

  

2nd (15 tries)

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    //learn morris post-order traversal!!! this is most difficult!!!
    
    vector<int> ans;
    vector<int> postorderTraversal(TreeNode *root) {
        //now I can‘t get the iterative postorder traversal
        //rethink!!!
        //now I just remember preorder traversal
        //preorder and inorder are not so differcut,just change ans.push_back() position!!!
        //postorder???
        /*
        stack<TreeNode*> s;
        vector<TreeNode*> roots;
        TreeNode *cur = root;
        while(!s.empty() || cur != NULL) {
            while(cur != NULL) {
                s.push(cur);
                cur = cur->left;
            }
            cur = s.top();
            //now the root have out of stack,how to save it???
            //This method change Tree structure???how to don‘t change tree structure!!!
            //using flag is one method!!!
            if(cur->right == NULL) {
                ans.push_back(cur->val);
                s.pop();
                cur = cur->right;
            }
            else {
                TreeNode *pre = cur;
                cur = cur->right;
                pre->right = NULL;
            }
        }
        return ans;
        */
        //use one stack to obtain postorderTraversal
        //root , right, left  then reverse the vector!!!
        //!!!
        /*
        stack<TreeNode*> s;
        stack<TreeNode*> post;
        TreeNode *cur = root;
        while( !s.empty() || cur != NULL ) {
            while(cur != NULL ) {
                ans.push_back(cur->val);
                s.push(cur);
                cur = cur->right;
            }
            cur = s.top();
            s.pop();
            cur = cur->left;
        }
        vector<int> res;
        while( !post.empty() ) {
            ans.push_back(post.top()->val);
            post.pop();
        }
        reverse(ans.begin(),ans.end());
        return ans;
        */
        //another method is using prev!!!
        //link:http://leetcode.com/2010/10/binary-tree-post-order-traversal.html
        if(root == NULL)
            return ans;
        stack<TreeNode*> s;
        s.push(root);
        TreeNode *cur = NULL,*pre = NULL;
        while( !s.empty() ) {
            cur = s.top();
            if( pre == NULL || pre->left == cur || pre->right == cur ) {
                if(cur->left != NULL)
                    s.push(cur->left);
                else if(cur->right != NULL)
                    s.push(cur->right);
            }
            else if( cur->left == pre ) {
                if( cur->right != NULL )
                    s.push(cur->right);
            }
            else {
                ans.push_back(cur->val);
                s.pop();
            }
            pre = cur;
        }
        return ans;
    }
    /*
    void dfs(TreeNode *root) {
        if(root == NULL)
            return;
        dfs(root->left);
        dfs(root->right);
        ans.push_back(root->val);
    }
    */
};

  

/**
 * 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> ans;
    void reverse(TreeNode *from,TreeNode *to) {
       TreeNode *cur = from->left;
       TreeNode *post = cur->right;
       //reverse covery like reverse link list...
       while( cur != to ) {
           TreeNode *post_right = post->right;
           post->right = cur;
           cur = post;
           post = post_right;
       }
       while( cur != from->left ) {
           ans.push_back(cur->val);
           cur = cur->right;
       }
       ans.push_back(cur->val);
    }
    void reversecover(TreeNode *from,TreeNode *to) {
        TreeNode *cur = from;
        TreeNode *post = cur->right;
        TreeNode *end = to->left;
        //just reverse like reverse link list
        while( cur != end ) {
            TreeNode *post_right = post->right;
            post->right = cur;
            cur = post;
            post = post_right;
        }
        from->right = to;
    }
    vector<int> postorderTraversal(TreeNode *root) {
        //morris
        TreeNode *newroot = new TreeNode(0);
        newroot->left = root;
        newroot->right = NULL;
        
        TreeNode *cur = newroot,*pre = NULL;
        while( cur != NULL ) {
            if( cur->left == NULL ) {
                cur = cur->right;
            }
            else {
                pre = cur->left;
                while( pre->right != NULL && pre->right != cur ) {
                    pre = pre->right;
                }
                if( pre->right == NULL ) {
                    pre->right = cur;
                    cur = cur->left;
                }
                else {
                    //post-order here
                    //reverse
                    reverse(cur,pre);
                    //reverse-cover
                    reversecover(pre,cur);
                    //over
                    pre->right = NULL;
                    cur = cur->right;
                }
            }
        }
        return ans;
    }
};

  

【leetcode】Binary Tree Postorder Traversal,布布扣,bubuko.com

【leetcode】Binary Tree Postorder Traversal

标签:des   blog   http   os   io   strong   for   ar   

原文地址:http://www.cnblogs.com/weixliu/p/3924390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!