LeetCode 1530. 好叶子节点对的数量

给定一棵二叉树和一个整数distance,好叶子节点对是指两个叶节点间最短路径长度小于等于distance的节点对。本题通过递归方法找到最近公共祖先并计算满足条件的叶子节点对数量。时间复杂度为O(N*distance^2),空间复杂度为O(H*distance)。
摘要由CSDN通过智能技术生成

1530. 好叶子节点对的数量

给你二叉树的根节点 root 和一个整数 distance

如果二叉树中两个 节点之间的 最短路径长度 小于或者等于 distance ,那它们就可以构成一组 好叶子节点对

返回树中 好叶子节点对的数量

示例 1:

img

输入:root = [1,2,3,null,4], distance = 3
输出:1
解释:树的叶节点是 3 和 4 ,它们之间的最短路径的长度是 3 。这是唯一的好叶子节点对。

示例 2:

img

输入:root = [1,2,3,4,5,6,7], distance = 3
输出:2
解释:好叶子节点对为 [4,5] 和 [6,7] ,最短路径长度都是 2 。但是叶子节点对 [4,6] 不满足要求,因为它们之间的最短路径长度为 4 。

示例 3:

输入:root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
输出:1
解释:唯一的好叶子节点对是 [2,5] 。

示例 4:

输入:root = [100], distance = 1
输出:0

示例 5:

输入:root = [1,1,1], distance = 2
输出:1

提示:

  • tree 的节点数在 [1, 2^10] 范围内。
  • 每个节点的值都在 [1, 100] 之间。
  • 1 <= distance <= 10

二、方法一

递归,先找最近公共祖先,然后统计距离不超过的

/**
 * 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 int countPairs(TreeNode root, int distance) {
        Pair pair = dfs(root, distance);
        return pair.count;
    }
    public Pair dfs(TreeNode root, int distance) {
        int[] depths = new int[distance + 1];
        boolean isLeaf = root.left == null && root.right == null;
        if (isLeaf) {
           depths[0] = 1;
           return new Pair(depths, 0);
        }
        int[] leftDepths = new int[distance + 1];
        int[] rightDepths = new int[distance + 1];
        int leftCount = 0;
        int rightCount = 0;
        if (root.left != null) {
            Pair left = dfs(root.left, distance);
            leftDepths = left.depths;
            leftCount = left.count;
        }
        if (root.right != null) {
            Pair right = dfs(root.right, distance);
            rightDepths = right.depths;
            rightCount = right.count;
        }
        for (int i = 0; i < distance; i++) {
            depths[i + 1] += leftDepths[i];
            depths[i + 1] += rightDepths[i];
        }
        int cnt = 0;
        for (int i = 0; i <= distance; i++) {
            for (int j = 0; j + i + 2 <= distance; j++) {
                cnt += leftDepths[i] * rightDepths[j];
            }
        }
        return new Pair(depths, cnt + leftCount + rightCount);
    }

    class Pair {
        int[] depths;
        int count;
        public Pair(int[] depths, int count) {
            this.depths = depths;
            this.count = count;
        }
    }
}

复杂度分析

  • 时间复杂度:O(N⋅distance2 ),其中 N 为树中节点的数量。对于每个节点,我们至多需要遍历 O(distance2) 种好叶子节点对。

  • 空间复杂度:O(H⋅distance),其中 H 为树的高度。对于每个节点,我们都需要额外开辟 O(distance) 的空间,而栈的最大深度为 O(H)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值