代码随想录——二叉树习题合集

 145. 二叉树的后序遍历

 vector<int> postorderTraversal(TreeNode* root) {
            vector<int> res;
            rearxx(root,res);
        return res;
    }
    void rearxx(TreeNode* root,vector<int> & res){
         if(root!=NULL){
            rearxx(root->left,res);
            rearxx(root->right,res);
            res.push_back(root->val);
         }
    }

 144.二叉树的前序遍历

 vector<int> preorderTraversal(TreeNode* root) {
         vector<int> res;
         prexx(root,res);
         return res;
    }
    void prexx(TreeNode* xx,vector<int>& res){
        if(xx!=NULL){
            res.push_back(xx->val);
            prexx(xx->left,res);
            prexx(xx->right,res);
        }
    }

94. 二叉树的中序遍历

vector<int> inorderTraversal(TreeNode* root) {
         vector<int> res;
         midxx(root,res);
         return res;
    }
    void midxx(TreeNode* xx,vector<int>& res){
        if(xx!=NULL){
              midxx(xx->left,res);
            res.push_back(xx->val);
            midxx(xx->right,res);
        }
    }

 102. 二叉树的层序遍历

 vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> res;
      //  int hh=0,tt=-1;
        if(root!=NULL) q.push(root);
        else return res;
        while(!q.empty()){
            vector<int> temp;
            int len =q.size();
            while(len--){
                auto aa=q.front();
                q.pop();
                temp.push_back(aa->val);
                if(aa->left!=NULL) q.push(aa->left);
                if(aa->right!=NULL) q.push(aa->right);
            }
            res.push_back(temp);
        }
        return res;
    }

 226. 翻转二叉树

TreeNode* invertTree(TreeNode* root) {
        if(root==NULL) return NULL;
        TreeNode* tree=new TreeNode();
      
        xx(root,tree);
   
        return tree;
    }
    void xx(TreeNode* root,TreeNode* tree){
        if(root!=NULL){
            if(root->right!=NULL)  tree->left=new TreeNode();
            xx(root->right,tree->left);
            tree->val=root->val;
             if(root->left!=NULL)  tree->right=new TreeNode();
            xx(root->left,tree->right);
        }
    }

101. 对称二叉树

 bool isSymmetric(TreeNode* root) {
        //TreeNode* tree=new TreeNode();
      //  tree=root;
        bool flag= xx(root,root);
       // cout<<"xx"<<flag<<endl;
        return flag;
    }
    bool xx(TreeNode* root,TreeNode* tree){
        if(root!=NULL&&tree!=NULL){
            if(!xx(root->left,tree->right)) return false;
            if(root->val!=tree->val) return false;
            if(!xx(root->right,tree->left)) return false;
            return true;
        }
        if(root==NULL&&tree==NULL) return true;
        else return false;
    }

 100. 相同的树

 bool isSameTree(TreeNode* p, TreeNode* q) {
        return  xx(p,q);
    }
    bool  xx (TreeNode* p, TreeNode* q){
        if(p!=NULL&&q!=NULL){
            if(p->val!=q->val) return false;
            
            if(!xx(p->left,q->left))  return false;
             
            if(!xx(p->right,q->right))  return false;
            
            return true;
        }
        if(p==NULL&&q==NULL) return true;
        else  return false;
        
    }

572. 另一棵树的子树

  bool isSubtree(TreeNode* root, TreeNode* subRoot) {
           return xx(root,subRoot);
    }
    bool xx(TreeNode* root,TreeNode* subRoot){
        if(root!=NULL){
           if(root->val==subRoot->val&&xxx(root,subRoot)) return true; 
           if(xx(root->left,subRoot))return true;
           if(xx(root->right,subRoot))return true;
           return false;
        }
        return false;
    }
    bool xxx(TreeNode* root,TreeNode* subRoot){
        if(root!=NULL&&subRoot!=NULL){
            if(root->val!=subRoot->val) return false;
            if(!xxx(root->left,subRoot->left)) return false;
            if(!xxx(root->right,subRoot->right)) return false;
            return true;
        }
        if(root==NULL&&subRoot==NULL) return true;
        else return false;
    }

 104. 二叉树的最大深度

int maxDepth(TreeNode* root) {
        int res=0;
        xx(root,res,1);
        return res;
    }
    void xx(TreeNode* root,int &res,int height){
        if(root!=NULL){
            res=max(res,height);
            xx(root->left,res,height+1);
            xx(root->right,res,height+1);
        }
    }

111. 二叉树的最小深度

int minDepth(TreeNode* root) {
        if(root==NULL) return 0;
            int res=1e5+10;
            xx(root,res,1);
            return res;
    }
   void xx(TreeNode* root,int& res,int height){
        if(root!=NULL){
            if(root->left==NULL&&root->right==NULL) res=min(res,height);
            xx(root->left,res,height+1);
            xx(root->right,res,height+1);
        }
    }

222. 完全二叉树的节点个数

int countNodes(TreeNode* root) {
            int res=0;
            xx(root,res);
            return res;
    }
    void xx(TreeNode* root,int& res){
        if(root!=NULL){
            ++res;
            xx(root->left,res);
            xx(root->right,res);
        }
    }

 *110. 平衡二叉树*

 bool ans=true;
    bool isBalanced(TreeNode* root) {
       dfs(root);
        return ans;
    }
    int dfs(TreeNode* root){
        if(root==NULL) return 0;
        int lh=dfs(root->left)+1,rh=dfs(root->right)+1;
        //cout<<lh<<"   "<<rh<<endl;
        if(abs(lh-rh)>1) ans=false;
        return max(lh,rh);
    }

257. 二叉树的所有路径

 vector<string> res;
    vector<string> binaryTreePaths(TreeNode* root) {
        string ss;
        xx(root,ss);
        return res; 
    }
    void xx(TreeNode* root,string ss){
        if(root!=NULL){
            ss+=to_string(root->val)+"->" ;
            if(root->left==NULL&&root->right==NULL) res.push_back(ss.substr(0,ss.size()-2));
            xx(root->left,ss);
            xx(root->right,ss);
        }
    }

 404. 左叶子之和

  int res=0;
    int sumOfLeftLeaves(TreeNode* root) {
        xx(root);
        return res;
    }
    int xx(TreeNode* root){
        if(root==NULL) return 0;
        res+=xx(root->left);
        xx(root->right);
        if(root->left==NULL&&root->right==NULL) return root->val;
       else return  0;
    }

 513. 找树左下角的值

int res,hh=0;
    int findBottomLeftValue(TreeNode* root) {
        xx(root,1);
        return res;
    }
    void xx(TreeNode* root,int height){
        if(root!=NULL){
            if(root->left==NULL&&root->right==NULL&&height>hh){
                res=root->val;
                hh=height;
            } 
            xx(root->left,height+1);
            xx(root->right,height+1);
        }
    }

 112. 路径总和

bool res=false;
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root==NULL) return false;
        xx(root,0,targetSum);
        return res;
    }
    void xx(TreeNode* root,long sum,int targetSum){
        if(root!=NULL&&res==false){
            sum+=root->val;
          //  cout<<sum<<endl;
            if(sum==targetSum&&root->left==NULL&&root->right==NULL) res=true;
            xx(root->left,sum,targetSum);
            xx(root->right,sum,targetSum);
        }
    }

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

 unordered_map<int,int> pos;
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        for(int i=0;i<inorder.size();i++) pos[inorder[i]]=i;
        auto root= xx(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
        return root;
    }
    TreeNode* xx(vector<int>&preorder,vector<int>&inorder,int pl,int pr,int il,int ir){
        if(pl>pr) return NULL;
        auto root=new TreeNode(preorder[pl]);
        int k=pos[root->val];
        root->left=xx(preorder,inorder,pl+1,pl+1+k-1-il,il,k-1);
        root->right=xx(preorder,inorder,pl+1+k-1-il+1,pr,k+1,ir);
        return root;
    }   

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

 unordered_map<int,int> pos;
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        for(int i=0;i<inorder.size();i++) pos[inorder[i]]=i;
        return xx(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1);
    }
    
    TreeNode* xx(vector<int>& inorder, vector<int>& postorder,int il,int ir,int pl,int pr){
        if(pl>pr) return NULL;
        auto root=new TreeNode(postorder[pr]);
        int k=pos[root->val];
        root->left=xx(inorder,postorder,il,k-1,pl,pl+k-1-il);
        root->right=xx(inorder,postorder,k+1,ir,pl+k-1-il+1,pl+k-1-il+1+ir-k-1);
        return root;
    }

654. 最大二叉树

 TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return  xx(nums,0,nums.size()-1);
    }
    TreeNode* xx(vector<int> & nums,int l ,int r){
        if(l>r) return NULL;
        auto root=new TreeNode();
        int loc=-1,mnum=-1;
       
        for(int i=l;i<=r;i++){
            if(nums[i]>mnum){
                mnum=nums[i];
                loc=i;
            }
        }
        
        root->val=mnum;
        root->left=xx(nums,l,loc-1);
        root->right=xx(nums,loc+1,r);
        return root;
    }

 617. 合并二叉树

 TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        return  xx(root1,root2);
    }
    TreeNode* xx(TreeNode* root1,TreeNode* root2){
        if(root1==NULL&&root2==NULL) return NULL;
        auto root=new TreeNode();
        if(root1==NULL&&root2!=NULL) {
            root->val=root2->val;
            root1=new TreeNode();
        }  
        else if(root2==NULL&&root1!=NULL){
            root->val=root1->val;
            root2=new TreeNode();
        }   
        else root->val=root1->val+root2->val;
        
        root->left=xx(root1->left,root2->left);
        root->right=xx(root1->right,root2->right);
        return root;
    }

 700. 二叉搜索树中的搜索

TreeNode* searchBST(TreeNode* root, int val) {
        return xx(root,val);
    }
    TreeNode* xx(TreeNode* root,int val){
        if(root!=NULL){
            if(root->val==val) return root;
            auto left=xx(root->left,val);
            auto right=xx(root->right,val);
            if(left!=NULL) return left;
            if(right!=NULL) return right;
        }
        return NULL;

    }

98. 验证二叉搜索树

long  ans=-2147483649;
    bool isValidBST(TreeNode* root) {
        return xx(root);
    }
    bool xx(TreeNode* root){
        if(root!=NULL){
            if(!xx(root->left)) return false;
            
            if(root->val<=ans){
                cout<<"xx"<<endl;
                return false;
            } 
            else ans=root->val;

            if(!xx(root->right)) return false;

            return true; 
        }
        return true;
    }

 530. 二叉搜索树的最小绝对差

 vector<int> aa;
    int getMinimumDifference(TreeNode* root) {
        int res=10000;
        xx(root);
        for(int i=0;i<aa.size()-1;i++){
            for(int j=i+1;j<aa.size();j++){
                res=min(res,abs(aa[i]-aa[j]));
            }
        }
        return res;
    }
    void xx(TreeNode* root){
        if(root!=NULL){
            aa.push_back(root->val);
            xx(root->left);
            xx(root->right);
        }
    }

 501. 二叉搜索树中的众数

 unordered_map<int,int> map;
    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        xx(root);
        int mnum=0;
        for(auto cc: map)  mnum=max(mnum,cc.second);
        for(auto cc: map) 
            if(cc.second==mnum) res.push_back(cc.first);
        
        return res;
    }
    void xx(TreeNode* root){
        if(root!=NULL){
            map[root->val]++;
            xx(root->left);
            xx(root->right);
        }
    }

 ****236. 二叉树的最近公共祖先*****

 TreeNode* ans;
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        xx(root,p,q);
        return ans;
    }
    int xx(TreeNode* root, TreeNode* p, TreeNode* q){
        if(root==NULL) return 0;
        int state=xx(root->left,p,q);
        if(root==p) state|=1;
        else if(root==q) state|=2;
        state|=xx(root->right,p,q);
        if(state==3&&!ans) ans=root;
        return state;
    }

 235. 二叉搜索树的最近公共祖先

 TreeNode* ans;
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(p->val>q->val) swap(p,q);
        if(p->val<=root->val&&q->val>=root->val) return root;
        if(q->val<root->val) return  lowestCommonAncestor(root->left,p,q);
        else return lowestCommonAncestor(root->right,p,q);
    }

 701. 二叉搜索树中的插入操作

 TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root==NULL) return new TreeNode(val);
        xx(root,val);
        return root;
    }
    TreeNode* xx(TreeNode* root,int val){
       if(root==NULL){
           auto cc=new TreeNode(val);
           return cc;
       }
       if(root->val<val) root->right=xx(root->right,val);
       else root->left=xx(root->left,val);
       return root;
    }

 450. 删除二叉搜索树中的节点

 TreeNode* deleteNode(TreeNode* root, int key) {
        xx(root,key);
        return root;
    }
    void xx(TreeNode*& root,int val){
        if(root==NULL) return ;

        if(root->val==val){
         if(!root->left&&!root->right) root=NULL; //无儿子
         else if(!root->left&&root->right)root=root->right;//一个右儿子
         else if(root->left&&!root->right)root=root->left;//一个左儿子
         else if(root->left&&root->right) {
            auto p =root->right;//两个儿子
            while(p->left) p=p->left;
            root->val=p->val;
            xx(root->right,p->val);
         } 
        }
        
        else  if(root->val<val) xx(root->right,val);
        else  xx(root->left,val);
    }

 669. 修剪二叉搜索树

 TreeNode* trimBST(TreeNode* root, int low, int high) {
        return xx(root,low,high);
    }
    TreeNode* xx(TreeNode* root,int low,int high){
        if(root==NULL)  return NULL;
        if(root->val<low) return xx(root->right,low,high);
        if(root->val>high) return xx(root->left,low,high);
        root->left=xx(root->left,low,high);
        root->right=xx(root->right,low,high);
        return root;
    }

 108. 将有序数组转换为二叉搜索树

 TreeNode* sortedArrayToBST(vector<int>& nums) {
        return  xx(nums,0,nums.size()-1);
    }
    TreeNode* xx(vector<int> nums,int l,int r){
        if(l>r) return NULL;
        auto root=new TreeNode();
        int loc=(l+r)/2;
        root->val=nums[loc];
        root->left=xx(nums,l,loc-1);
        root->right=xx(nums,loc+1,r);
        return root;
    }

 538. 把二叉搜索树转换为累加树

int sum=0;
    TreeNode* convertBST(TreeNode* root) {
        xx(root);
        return root;
    }
    void xx(TreeNode*& root){
      if(!root) return ;
      xx(root->right);
      int x=root->val;
      root->val+=sum;
      sum+=x;
      xx(root->left);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值