力扣labuladong——一刷day47

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣993. 二叉树的堂兄弟节点](#一、力扣993. 二叉树的堂兄弟节点)
  • [二、力扣1315. 祖父节点值为偶数的节点和](#二、力扣1315. 祖父节点值为偶数的节点和)
  • [三、力扣1448. 统计二叉树中好节点的数目](#三、力扣1448. 统计二叉树中好节点的数目)
  • [四、力扣1469. 寻找所有的独生节点](#四、力扣1469. 寻找所有的独生节点)

前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题需要用到「遍历」的思维。

一、力扣993. 二叉树的堂兄弟节点

java 复制代码
/**
 * 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 {
    int depthX = -1, depthY = -1;
    boolean flag = true;
    public boolean isCousins(TreeNode root, int x, int y) {
        fun(root,1,x,y);
        if(flag == false){
            return false;
        }
        if(depthX == depthY){
            return true;
        }
        return false;
    }
    public void fun(TreeNode root, int depth, int x, int y){
        if(root == null){
            return ;
        }
        if(root.val == x){
            depthX = depth;
        }
        if(root.val == y){
            depthY = depth;
        }
        if(root.left != null && root.right != null){
            if((root.left.val == x || root.left.val == y) && (root.right.val == x || root.right.val == y)){
                flag = false;
            }
        }
        fun(root.left,depth+1,x,y);
        fun(root.right,depth+1,x,y);
    }
}

二、力扣1315. 祖父节点值为偶数的节点和

java 复制代码
/**
 * 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 {
    int sum = 0;
    public int sumEvenGrandparent(TreeNode root) {
        fun(root, null, null);
        return sum;
    }
    public void fun(TreeNode root, TreeNode parent, TreeNode grandParent){
        if(root == null){
            return;
        }
        if(grandParent != null && grandParent.val % 2 == 0){
            sum += root.val;
        }
        fun(root.left, root, parent);
        fun(root.right, root, parent);
    }
}

三、力扣1448. 统计二叉树中好节点的数目

java 复制代码
/**
 * 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 {
    int count = 0;
    public int goodNodes(TreeNode root) {
        fun(root,Integer.MIN_VALUE);
        return count;
    }
    public void fun(TreeNode root, int preMax){
        if(root == null){
            return;
        }
        if(root.val >= preMax){
            preMax = root.val;
            count ++;
        }
        fun(root.left, preMax);
        fun(root.right, preMax);
    }
}

四、力扣1469. 寻找所有的独生节点

java 复制代码
/**
 * 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 {
    List<Integer> res = new ArrayList<>();
    public List<Integer> getLonelyNodes(TreeNode root) {
        fun(root);
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        if(root.left == null && root.right != null){
            res.add(root.right.val);
        }
        if(root.left != null && root.right == null){
            res.add(root.left.val);
        }
        fun(root.left);
        fun(root.right);
    }
}
相关推荐
南山十一少1 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
427724003 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo3 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦3 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个3 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^4 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
大数据追光猿5 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!6 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉6 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生6 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode