【CODE】树——牛客·剑指offer

重建二叉树

https://cloud.tencent.com/developer/article/1401290

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        TreeNode* root;
        root=reBuild(pre,0,pre.size()-1,vin,0,vin.size()-1);
        return root;
    }
    TreeNode* reBuild(vector<int> pre,int pres,int pree,vector<int> vin,int vins,int vine){
        if(pres>pree || vins>vine) return NULL;
        TreeNode* root=new TreeNode(pre[pres]);
        //root->val=pre[pres];
        for(int i=vins;i<=vine;i++){
            if(vin[i]==pre[pres]){
                root->left=reBuild(pre,pres+1,pres+i-vins,vin,vins,i-1);
                root->right=reBuild(pre,pres+i-vins+1,pree,vin,i+1,vine);
            }
        }
        return root;
    }
};

二叉树的下一个节点

给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。

/*
struct TreeLinkNode {
    int val;
    struct TreeLinkNode *left;
    struct TreeLinkNode *right;
    struct TreeLinkNode *next;
    TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
        
    }
};
*/
class Solution {
public:
    TreeLinkNode* GetNext(TreeLinkNode* pNode){
        if(pNode->right!=NULL){
            TreeLinkNode* tmp=pNode->right;
            while(tmp->left!=NULL) tmp=tmp->left;
            return tmp;
        }else{
            if(pNode->next!=NULL){
                if(pNode==pNode->next->left) return pNode->next;
                else{
                    TreeLinkNode* tmp=pNode->next;
                    while(tmp->next!=NULL){
                        if(tmp->next->left!=NULL && tmp==tmp->next->left) return tmp->next;
                        else tmp=tmp->next;
                    }
                    return NULL;
                }
            }
        }
        return NULL;
    }
};
  • 有右子树:返回右子树的最左节点
  • 无右子树:
  • 是父节点的左子树,返回父节点
  • 是父节点的右子树:
  • 父节点->父节点->……是谁的左子树,返回“谁”
  • 父节点->父节点->……不是谁的左子树,返回NULL

对称的二叉树

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    bool isSame(TreeNode* left,TreeNode* right){
        if(left==NULL && right==NULL) return true;
        else if(left!=NULL && right!=NULL){
            if(left->val!=right->val) return false;
            else return isSame(left->left,right->right) && isSame(left->right,right->left);
        }else return false;
    }
    bool isSymmetrical(TreeNode* pRoot){
        if(pRoot==NULL) return true;
        return isSame(pRoot->left,pRoot->right);
    }

};
  • 务必判断pRoot为空的情况,为true

按之字形顺序打印二叉树

请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > res;
        queue<TreeNode*> q1,q2;
        if(pRoot==NULL) return res;
        q1.push(pRoot);
        res.push_back({pRoot->val});
        int t=1,e=0;
        vector<int> tmp;
        while(!q1.empty() || !q2.empty()){
            while(!q1.empty()){
                TreeNode* p=q1.front();
                q1.pop();
                if(q1.empty()) e=1;
                else e=0;
                if(p->left){
                    tmp.push_back(p->left->val);
                    q2.push(p->left);
                }
                if(p->right){
                    tmp.push_back(p->right->val);
                    q2.push(p->right);
                }
                if(tmp.size()!=0){
                    if(e==1){
                        reverse(tmp.begin(),tmp.end());
                        res.push_back(tmp);
                        tmp.clear();
                    }
                }
            }
            while(!q2.empty()){
                TreeNode* p=q2.front();
                q2.pop();
                if(q2.empty()) e=1;
                else e=0;
                if(p->left){
                    tmp.push_back(p->left->val);
                    q1.push(p->left);
                }
                if(p->right){
                    tmp.push_back(p->right->val);
                    q1.push(p->right);
                }
                if(tmp.size()!=0){
                    if(e==1){
                        res.push_back(tmp);
                        tmp.clear();
                    }
                }
            }
        }
        return res;
    }
};

把二叉树打印成多行

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            queue<TreeNode*> q1,q2;
            vector<vector<int> >res;
            if(pRoot==NULL) return res;
            q1.push(pRoot);
            while(!q1.empty() || !q2.empty()){
                vector<int> t;
                while(!q1.empty()){
                    TreeNode* tmp=q1.front();
                    q1.pop();
                    t.push_back(tmp->val);
                    if(tmp->left) q2.push(tmp->left);
                    if(tmp->right) q2.push(tmp->right);
                    if(q1.empty()){
                        res.push_back(t);
                        t.clear();
                    }
                }
                while(!q2.empty()){
                    TreeNode* tmp=q2.front();
                    q2.pop();
                    t.push_back(tmp->val);
                    if(tmp->left) q1.push(tmp->left);
                    if(tmp->right) q1.push(tmp->right);
                    if(q2.empty()){
                        res.push_back(t);
                        t.clear();
                    }
                }
            }
            return res;
        }
    
};
/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            queue<TreeNode*> q;
            vector<vector<int> >res;
            if(pRoot==NULL) return res;
            q.push(pRoot);
            int start=0,end=1;
            vector<int> t;
            while(!q.empty()){
                TreeNode* tmp=q.front();
                q.pop();
                t.push_back(tmp->val);
                start++;
                if(tmp->left) q.push(tmp->left);
                if(tmp->right) q.push(tmp->right);
                if(start==end){
                    res.push_back(t);
                    t.clear();
                    start=0;
                    end=q.size();
                }
            }
            return res;
        }
    
};
  • 只用一个队列,利用start和end判断是否在同一层

二叉搜索树的第k个节点

给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8)    中,按结点数值大小顺序第三小结点的值为4。

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    //二叉搜索树,中序遍历,非递归写法,栈
    TreeNode* KthNode(TreeNode* pRoot, int k){
        if(pRoot==NULL) return NULL;
        stack<TreeNode*> s;
        TreeNode* tmp=pRoot;
        int i=0;
        while(tmp!=NULL || !s.empty()){
            while(tmp!=NULL){
                s.push(tmp);
                tmp=tmp->left;
            }
            if(!s.empty()){
                TreeNode *t=s.top();
                s.pop();
                i++;
                if(i==k) return t;
                tmp=t->right;
            }
        }
        return NULL;
    }
};
  • 最后跳出while后,没有return NULL,牛客网上报错“运行超时”???。。。
发布了133 篇原创文章 · 获赞 35 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Li_Jiaqian/article/details/104153780
今日推荐