zoukankan      html  css  js  c++  java
  • LeetCode "Binary Tree *-order Traversal' by Iteration

    Binary Tree *-order traversal by recursion is trivial. But their iteration version deserves a look:

    Pre-Order

    class Solution 
    {
        vector<int> ret;
    
    public:
        
        vector<int> preorderTraversal(TreeNode *p) 
        {
            if (!p) return ret;
    
            stack<TreeNode *> stk;
            stk.push(p);
            while (!stk.empty())
            {
                TreeNode *pTop = stk.top(); stk.pop();
    
                ret.push_back(pTop->val);
                
                if (pTop->right) stk.push(pTop->right);
                if (pTop->left) stk.push(pTop->left);
            }
            return ret;
        }
    };

    In-Order

    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode *root) 
        {
            vector<int> ret;
            if (!root) return ret;
            
            unordered_set<TreeNode*> bk;
            stack<TreeNode *> stk;
            stk.push(root);
            
            while (!stk.empty())
            {
                TreeNode *p = stk.top();
                if (p->left && bk.find(p->left) == bk.end())
                {
                    stk.push(p->left);
                    continue;
                }
                ret.emplace_back(p->val);
                stk.pop();
                bk.insert(p);            
                
                if (p->right)
                {
                    stk.push(p->right);
                }
            }
    
            return ret;
        }
    };

    Post-Order - DIFFICULT

    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode *root) 
        {
            vector<int> ret;
            if (!root) return ret;
            
            //    Init
            stack<TreeNode *> stk;
            stk.push(root);
            TreeNode *pPre = nullptr;
            
            //    Go
            while (!stk.empty())
            {
                TreeNode *pCurr = stk.top(); 
                //    Down: from parent to child
                if (!pPre || pPre->left == pCurr || pPre->right == pCurr)
                {
                    if (pCurr->left)
                        stk.push(pCurr->left);
                    else if (pCurr->right)
                        stk.push(pCurr->right);
                }
                //    Up: from left child
                else if (pCurr->left == pPre)
                {
                    if (pCurr->right)
                        stk.push(pCurr->right);
                }
                //    Up: from right child
                else
                {
                    ret.push_back(pCurr->val);
                    stk.pop();
                }
                pPre = pCurr;
            }
            return ret;
        }
    };
  • 相关阅读:
    从Android源码修改cpu信息
    lintcode-->翻转字符串
    lintcode-->哈希函数
    PCP架构设计
    PCP项目立项
    linux下wc功能的简单实现
    goahead3.6.3就基本使用(后台上传信息到html页面),高手请忽略
    四则运算生成器
    快速看完软件工程教材后,我的疑惑
    软件工程学习
  • 原文地址:https://www.cnblogs.com/tonix/p/4166262.html
Copyright © 2011-2022 走看看