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

二叉树前中后序非递归遍历

时间:2020-09-16 12:19:36      阅读:29      评论:0      收藏:0      [点我收藏+]

标签:==   ||   efi   val   lse   tac   init   node   int   

/**
 * 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>res;

public:
    void PreorderTraversal(TreeNode* root) 
    {
       stack<TreeNode*>sstack;
        sstack.push(root);
        while( !sstack.empty() )
        {
            TreeNode* temp=sstack.top();
            sstack.pop();
            res.push_back( temp->val );
            if(temp->right)
            {
                sstack.push( temp->right );
            }
            if(temp->left)
            {
                sstack.push( temp->left );
            }
        }
    }

    vector<int> preorderTraversal(TreeNode* root) {
        if(root==NULL)
        {
            return {};
        }
        PreorderTraversal(root);
        return res;
    }
};

  

/**
 * 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> res;
public:
    void InorderTraversal(TreeNode *root)
    {
       stack<TreeNode*>sstack;
       TreeNode*head=root;
       while( !sstack.empty()|| head!=NULL )
       {
            if(head!=NULL)
            {
                
                sstack.push(head);
                head=head->left;
            }
            else
            {
                head=sstack.top();
                sstack.pop();
                res.push_back( head->val );
                //cout<<head->val<<endl;
                head=head->right;
            }
       }

    }

    vector<int> inorderTraversal(TreeNode* root) {
        if(root==NULL) return {};
        InorderTraversal(root);
        return res;
    }
};

  

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int>res;
    stack<int>sstackres;
public:
    void PostorderTraversal(TreeNode* root)
    {
        
        stack<TreeNode*>sstack;
        sstack.push(root);
        while( !sstack.empty() )
        {
            TreeNode* temp=sstack.top();
            sstack.pop();
            sstackres.push(temp->val);
            if(temp->left)
            {
                sstack.push( temp->left );
            }
            if(temp->right)
            {
                sstack.push( temp->right );
            }
        }

    }

    vector<int> postorderTraversal(TreeNode* root) {
        if(root==NULL) return {};
        PostorderTraversal(root);
        while( !sstackres.empty() )
        {
            res.push_back( sstackres.top() );
            sstackres.pop();
        }
        return res;
    }
};

  

二叉树前中后序非递归遍历

标签:==   ||   efi   val   lse   tac   init   node   int   

原文地址:https://www.cnblogs.com/RainzzZ/p/13603954.html

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