树的前中后非递归遍历

/**
 *  * 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> preorderTraversal(TreeNode* root) {
                vector<int> vec;
                if(root == NULL)
                {
                    return vec;
                }
                stack<TreeNode*> sta;
                while(1)
                {
                    while(root != NULL)
                    {
                        sta.push(root);
                        vec.push_back(root->val);
                        root = root->left;
                    }
                    if(sta.empty() == true)
                    {
                        break;
                    }
                    TreeNode *temp = sta.top();
                    sta.pop();
                    if(temp->right != NULL)
                    {
                        root = temp->right;
                    }

                }
                return vec;
                    }
};
--------------------------------------------------------------------------------------
class Solution {
    public:
            vector<int> inorderTraversal(TreeNode* root) {
                vector<int> vec;
                if(root == NULL)
                {
                    return vec;
                }
                stack<TreeNode*> sta;
                while(1)
                {
                    while(root != NULL)
                    {
                        sta.push(root);
                        root = root->left;
                    }
                    if(sta.empty() == true)
                    {
                        break;
                    }
                    TreeNode *pop = sta.top();
                    vec.push_back(pop->val);
                    sta.pop();
                    if(pop->right != NULL)
                    {
                        root = pop->right;
                    }
                }
                    }
};
----------------------------------------------------------------------------------------
class Solution {
    public:
            vector<int> postorderTraversal(TreeNode* root) {
                vector<int> vec;
                if(root == NULL)
                {
                    return vec;
                }
                TreeNode *bj = NULL;
                stack<TreeNode *> sta;
                TreeNode * tree = root;
                while(1)
            	{
            		while(tree)   //  把 所有的左 放到栈里
            		{	
            
            			//Push(&pTop,tree);       //  吧这个节点放到栈里
            			sta.push(tree);
            			tree = tree->left;
            		}
            		
            		//  栈如果是NULL  结束
            		if (sta.empty() == true)
            	    {
            	        break;
            	    }
            		//   通过栈顶元素  遍历右子树
            		if (sta.top()->right == NULL || sta.top()->right == bj)
            		{
            			//  把这个节点删除
            			bj = sta.top();
            			sta.pop();
            			vec.push_back(bj->val);
            		}
            		else
            		{
            			//  通过栈顶元素  遍历右子树
            			tree = sta.top()->right;
            		}
	}
                return vec;
            }
};

你可能感兴趣的:(树的前中后非递归遍历)