二叉树递归套路(树形DP)


打家劫舍 III

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    class Info{
        public:
        int headYes;
        int headNo;
        Info():headYes(0),headNo(0){}
        Info(int a,int b):headYes(a),headNo(b){}
    };
    Info process(TreeNode* x){
        if(x==nullptr){
            return Info(-1,-1);
        }
        Info leftInfo=process(x->left);
        Info rightInfo=process(x->right);
        int headYes=0;
        int headNo=0;
        if(leftInfo.headYes==-1&&rightInfo.headYes==-1){//-1表示空节点
            headYes=x->val;
            headNo=0;
        }else if(leftInfo.headYes==-1){
            headYes=rightInfo.headNo+x->val;
            headNo=max(rightInfo.headYes,rightInfo.headNo);
        }else if(rightInfo.headYes==-1){
            headYes=leftInfo.headNo+x->val;
            headNo=max(leftInfo.headYes,leftInfo.headNo);
        }else {
           headYes=leftInfo.headNo+rightInfo.headNo+x->val;
           headNo=max(leftInfo.headNo,leftInfo.headYes)+max(rightInfo.headNo,rightInfo.headYes);
        }
        return Info(headYes,headNo);
    }
    int rob(TreeNode* root) {
        Info info=process(root);
        return max(info.headYes,info.headNo);
    }
};

在这里插入图片描述


二叉树的直径


在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public class Info{
        public int height;
        public int maxPath;
        public Info(int a,int b){
            height=a;
            maxPath=b;
        }
    }
    public Info process(TreeNode x){
        if(x==null)return null;
        Info leftInfo=process(x.left);
        Info rightInfo=process(x.right);
        int height=0,maxPath=0;
        height=Math.max((leftInfo==null?0:leftInfo.height),(rightInfo==null?0:rightInfo.height))+1;
        maxPath=(leftInfo==null?0:leftInfo.height)+(rightInfo==null?0:rightInfo.height);
        maxPath=Math.max(maxPath,Math.max((leftInfo==null?0:leftInfo.maxPath),(rightInfo==null?0:rightInfo.maxPath)));
        return new Info(height,maxPath);
    }
    public int diameterOfBinaryTree(TreeNode root) {
        return process(root).maxPath;
    }
}

在这里插入图片描述



二叉树的坡度


在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述



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


在这里插入图片描述

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public class Info{
        public boolean isFindP;
        public boolean isFindQ;
        public TreeNode ancestor;
        public Info(boolean a,boolean b,TreeNode c){
            isFindP=a;
            isFindQ=b;
            ancestor=c;
        }
    }
    public Info process(TreeNode x,TreeNode p,TreeNode q){
        if(x==null){
            return null;
        }
        Info leftInfo=process(x.left,p,q);
        Info rightInfo=process(x.right,p,q);
        boolean isFindP=false;
        boolean isFindQ=false;
        TreeNode ancestor=null;
        isFindP=(leftInfo==null?false:leftInfo.isFindP)|(rightInfo==null?false:rightInfo.isFindP)|(x==p);
        isFindQ=(leftInfo==null?false:leftInfo.isFindQ)|(rightInfo==null?false:rightInfo.isFindQ)|(x==q);
        if(leftInfo!=null&&leftInfo.isFindP&&leftInfo.isFindQ){
            ancestor=leftInfo.ancestor;
        }
        if(rightInfo!=null&&rightInfo.isFindP&&rightInfo.isFindQ){
            ancestor=rightInfo.ancestor;
        }
        if(leftInfo!=null&&rightInfo!=null&&leftInfo.isFindP&&rightInfo.isFindQ){
            ancestor=x;
        }
        if(leftInfo!=null&&rightInfo!=null&&leftInfo.isFindQ&&rightInfo.isFindP){
            ancestor=x;
        }
        if(leftInfo!=null&&leftInfo.isFindP&&x==q){
            ancestor=x;
        }
        if(leftInfo!=null&&leftInfo.isFindQ&&x==p){
            ancestor=x;
        }
        if(rightInfo!=null&&rightInfo.isFindP&&x==q){
            ancestor=x;
        }
        if(rightInfo!=null&&rightInfo.isFindQ&&x==p){
            ancestor=x;
        }
        return new Info(isFindP,isFindQ,ancestor);
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return process(root,p,q).ancestor;
    }
}

在这里插入图片描述



二叉树的最近公共祖先


在这里插入图片描述

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public class Info{
        boolean isFindP;
        boolean isFindQ;
        TreeNode ancestor;
        public Info(boolean a,boolean b,TreeNode c){
            isFindP=a;
            isFindQ=b;
            ancestor=c;
        }
    }
    public Info process(TreeNode x,TreeNode p,TreeNode q){
        if(x==null){
            return new Info(false,false,null);
        }
        Info leftInfo=process(x.left,p,q);
        Info rightInfo=process(x.right,p,q);
        boolean isFindP=false;
        boolean isFindQ=false;
        TreeNode ancestor=null;
        isFindP=(leftInfo==null?false:leftInfo.isFindP)|(rightInfo==null?false:rightInfo.isFindP)|(x==p);
        isFindQ=(leftInfo==null?false:leftInfo.isFindQ)|(rightInfo==null?false:rightInfo.isFindQ)|(x==q);
        //p,q同时在x的左子树或者右子树上
        if(leftInfo!=null&&leftInfo.isFindP&&leftInfo.isFindQ){
            ancestor=leftInfo.ancestor;
        }
        if(rightInfo!=null&&rightInfo.isFindP&&rightInfo.isFindQ){
            ancestor=rightInfo.ancestor;
        }
        //p,q分别在x的左子树或者右子树上
        if(leftInfo!=null&&rightInfo!=null&&leftInfo.isFindP&&rightInfo.isFindQ){
            ancestor=x;
        }
        if(leftInfo!=null&&rightInfo!=null&&leftInfo.isFindQ&&rightInfo.isFindP){
            ancestor=x;
        }
       // p,q只能有一个在x的左子树或者右子树上
        if(leftInfo!=null&&leftInfo.isFindP&&x==q){
            ancestor=x;
        }
        if(leftInfo!=null&&leftInfo.isFindQ&&x==p){
            ancestor=x;
        }
        if(rightInfo!=null&&rightInfo.isFindP&&x==q){
            ancestor=x;
        }
        if(rightInfo!=null&&rightInfo.isFindQ&&x==p){
            ancestor=x;
        }
        return new Info(isFindP,isFindQ,ancestor);
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return process(root,p,q).ancestor;
    }
}

在这里插入图片描述



检查平衡性


在这里插入图片描述

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public class Info{
        public boolean isBalanced;
        public int height;
        public Info(boolean a,int b){
            isBalanced=a;
            height=b;
        }
    }
    public Info process(TreeNode x){
        if(x==null){
            return new Info(true,0);
        }
        Info leftInfo=process(x.left);
        Info rightInfo=process(x.right);
        int height=0;
        boolean isBalanced=false;
        height=Math.max((leftInfo==null?0:leftInfo.height),(rightInfo==null?0:rightInfo.height))+1;
        if((leftInfo==null?true:leftInfo.isBalanced)&&
            (rightInfo==null?true:rightInfo.isBalanced)&&
            Math.abs((leftInfo==null?0:leftInfo.height)-(rightInfo==null?0:rightInfo.height))<=1){
            isBalanced=true;
        }

        return new Info(isBalanced,height);
    }
    public boolean isBalanced(TreeNode root) {
        Info info=process(root);
        return info.isBalanced;
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值