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

145. Binary Tree Postorder Traversal

时间:2019-12-21 11:30:27      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:倒数   return   http   tree node   push   ++   problem   代码   efi   

  • 后序遍历二叉树(非递归实现)
    题目来源

  • C++代码实现

class Solution
{
public:
    vector<int> postorderTraversal(TreeNode* root)
    {
        vector<int> result;
        stack<TreeNode*> datastack1;
        stack<TreeNode*> datastack2;

        if (root != NULL)
        {
            datastack1.push(root);

            while (!datastack1.empty())
            {
                root = datastack1.top();
                datastack2.push(root);
                datastack1.pop();
                if (root->left != NULL)
                {
                    datastack1.push(root->left);
                }
                if (root->right != NULL)
                {
                    datastack1.push(root->right);
                }
            }
        }
        int length = datastack2.size();
        for (int i = 0; i < length; i++)
        {
            result.push_back((datastack2.top())->val);
            datastack2.pop();
        }

        return result;
    }
};

将最后的datastack2result中倒数据改为如下代码也可:

        while(!datastack2.empty())
        {
            result.push_back( (datastack2.top() )->val);
            datastack2.pop();
        }
        
  • 第一次出现BUG:
/**
 * Definition for a binary tree node.
 * 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;
        stack<TreeNode*> datastack1;
        stack<TreeNode*> datastack2;
        
        if(root != NULL)
        {
            datastack1.push(root);
            
            while(!datastack1.empty())
            {
                root = datastack1.top();
                datastack2.push(root);
                datastack1.pop();
                if(root->left != NULL)
                {
                    datastack1.push(root->left);
                }
                if(root->right != NULL)
                {
                    datastack1.push(root->right);
                }
            }
        }
        
        for(int i = 0;i < datastack2.size();i++)
        {
            result.push_back( (datastack2.top() )->val);
            datastack2.pop();
        }
        
        return result;
    }
};

最后发现BUG出现在

        for(int i = 0;i < datastack2.size();i++)
        {
            result.push_back( (datastack2.top() )->val);
            datastack2.pop();
        }

datastack2每pop出一次数据,datastack2.size()就会减一,同时i++,导致过早的结束了这个循环,正确的写法如下:

        int length = datastack2.size();
        for (int i = 0; i < length; i++)
        {
            result.push_back((datastack2.top())->val);
            datastack2.pop();
        }

145. Binary Tree Postorder Traversal

标签:倒数   return   http   tree node   push   ++   problem   代码   efi   

原文地址:https://www.cnblogs.com/Manual-Linux/p/12076160.html

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