leetcode :二叉树的前中后序遍历非递归实现,以及根据前中后序构造二叉树

/**
 * 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> ret;
        if(root==NULL)
            return ret;
        TreeNode* p;
        stack<TreeNode*> s;
        s.push(root);
        while(!s.empty()){
            p=s.top();
            s.pop();
            ret.push_back(p->val);
            if(p->right!=NULL)
                s.push(p->right);
            if(p->left!=NULL)
                s.push(p->left);
        }
        return ret;
    }
};

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

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

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ret;
        if(root==NULL)
            return ret;
        stack<TreeNode *> s;
        s.push(root);
        TreeNode* pre=NULL;//前一个输出结点
        TreeNode* curr=root;
        while(!s.empty()){
            curr=s.top();
            //  叶子结点 或者 它的子结点已经输出了(它有右结点因为先压右,那么curr->right==pre,否则curr->left==pre)
            if( (curr->left==NULL && curr->right==NULL) || (pre != NULL) && (curr->left==pre || curr->right==pre) ){
                ret.push_back(curr->val);
                pre=curr;
                s.pop();
            }
            else{
                if(curr->right!=NULL)
                    s.push(curr->right);
                if(curr->left!=NULL)
                    s.push(curr->left);
            }
        }
    }
};


class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size()==0)
            return NULL;
        return dfs(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
    }
    TreeNode* dfs(vector<int>::iterator inStart,vector<int>::iterator inEnd,vector<int>::iterator pStart,vector<int>::iterator pEnd){
        if(inStart==inEnd)
            return NULL;
        TreeNode* root=new TreeNode(*(pEnd-1));
        vector<int>::iterator it=find(inStart,inEnd,root->val);
        int leftSize=it-inStart;
        root->left=dfs(inStart,it,pStart,pStart+leftSize);
        root->right=dfs(it+1,inEnd,pStart+leftSize,pEnd-1);
        return root;
    }
};


前序与中序构造二叉树

 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size()==0)
            return NULL ;
        return dfs(preorder.begin(),preorder.end(),inorder.begin(),inorder.end());
    }
    //不包括preEnd,inEnd
    TreeNode* dfs(vector<int>::iterator preStart,vector<int>::iterator preEnd,vector<int>::iterator inStart,vector<int>::iterator inEnd) {
        if(preStart==preEnd)
            return NULL;
        TreeNode* root=new TreeNode(*preStart);
        vector<int>::iterator it=find(inStart,inEnd,root->val);
        int leftSize=it-inStart;
        root->left=dfs(preStart+1,preStart+1+leftSize,inStart,it);
        root->right=dfs(preStart+1+leftSize,preEnd,it+1,inEnd);
        return root;
    }
};



二叉树遍历使用场景:

先序遍历:在第一次遍历到节点时就执行操作,一般只是想遍历执行操作(或输出结果)可选用先序遍历;

中序遍历:对于二分搜索树,中序遍历的操作顺序(或输出结果顺序)是符合从小到大(或从大到小)顺序的,故要遍历输出排序好的结果需要使用中序遍历

后序遍历:后续遍历的特点是执行操作时,肯定已经遍历过该节点的左右子节点,故适用于要进行破坏性操作的情况,比如删除所有节点


作者:Entronad
链接:https://www.zhihu.com/question/22031935/answer/153859490
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


层次遍历用队列:

http://blog.csdn.net/le119126/article/details/49070317判断二叉树是否平衡、是否完全二叉树、是否二叉排序树

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值