[C++]leetcode 二叉树总结

1.从中序与后序遍历序列构造二叉树

class Solution{
public:
    TreeNode* buildTree(vector<int>& inorder,vector<int>& postorder){
        if(inorder.size()==0||postorder.size()==0) return Null;
        return build(inorder,0,inorder.size()-1,postorder,0,postorder()-1);
    build(vector<int>& inorder, int is, int ie,vector<int>& postorder , int ps ,int pe){
        TreeNode* root = new TreeNode(postorder[pe]);
        int i = is;
        while(i<ie&&postorder[pe]!=inorder[i])
        {
            i++;
        }
        int leftLen = i-is;
        int rightLen = ie - i;
        if(leftLen>0){
            roo->left = build(inorder,is,is+leftLen-1,postorder,ps,ps+leftLen-1);
        }
        if(rightLen>0)
        {
            root->right = build(inorder,is+leftLen+1,ie,postorder,ps+leftLen,pe-1);
        }
    return root;
}

2.从前序与中序遍历序列构造二叉树

class Solution{
public:
    TreeNode* buildTree(vector<int>&preorder,vector<int>& inorder){
        if(preorder.size()==0||inorder.size()==0) return NULL;
        return build(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1);
    }
    TreeNode* build(vector<int>&preorder,int ps.int pe,vector<int>&inorder,int is,int ie){
        TreeNode* root = new TreeNode(preorder[ps]);
        int i =is;
        while(i<ie&&preorder[ps]!=inorder[i])
        {
            i++;
        }
        int leftLen = i-is;
        int rightLen = ie - i;
        
        if(leftLen>0)
        {
            root->left = build(preorder,ps+1,ps+leftLen,inorder,is,is+leftLen-1);
        }
        if(rightLen>0)
        {
            root->right = build(preorder,ps+1+leftLen,pe,inorder,is+1+leftLen,ie);
        }
        return root;
    }
};

3.填充每个节点的下一个右侧节点指针(完美二叉树)

class Solution{
public:
    void createconnect(Node* Left,Node* Right){
        if(Left==nullptr||Left->next==Right) return;
        Left->next=Right;
        createconnect(Left->left,Left->right);
        createconnect(Left->right,Right->left);
        createconnect(Right->left,Right->right);
    }
    Node* connect(Node* root){
        if(root!=nullptr) createconnect(root->left,root->right);
        return root;
    }
}

4.填充每个节点的下一个右侧节点指针(非完美二叉树)

class Solution{
public:
    Node* connect(Node* root){
        if(root==nullptr) return NULL;
        if(root->left!=nullptr){
            root->left->next = (root->right!=nullptr)? root->right;getNext(root->Next);
        }
        if(root->right!=nullptr){
            root->right->next = getNext(root->Next);
        }
        connect(root->right);
        connect(root->left);
        return root;
        }
    Node* getNext(Node* uncle){
        if(uncle==nullptr) return NULL;
        if(uncle->left!=nullptr) return uncle->left;
        if(uncle->right!=nullptr) return uncle->right;
        return getNext(uncle->next);
    }
};
    

5.二叉树最近公共祖先

class Solution{
public: 
    TreeNode* lowestCommonAncestor(TreeNode* root,TreeNode*p,TreeNode*q){
        if(root==nullptr||root==q||root==p) return root;
        TreeNode* left= lowestCommonAncestor(root->left,p,q);
        TreeNode* right = lowestCommonAncestor(root->right,p,q);
        if(left==nullptr) return right;
        if(right==nullptr) return left;
        return root;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值