首页 > 编程语言 >LeetCode 1530. Number of Good Leaf Nodes Pairs

LeetCode 1530. Number of Good Leaf Nodes Pairs

时间:2024-07-15 11:40:43浏览次数:12  
标签:distance Pairs Good TreeNode val tree good Leaf root

原题链接在这里:https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/description/

题目:

You are given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance.

Return the number of good leaf node pairs in the tree.

Example 1:

Input: root = [1,2,3,null,4], distance = 3
Output: 1
Explanation: The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.

Example 2:

Input: root = [1,2,3,4,5,6,7], distance = 3
Output: 2
Explanation: The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.

Example 3:

Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
Output: 1
Explanation: The only good pair is [2,5].

Constraints:

  • The number of nodes in the tree is in the range [1, 210].
  • 1 <= Node.val <= 100
  • 1 <= distance <= 10

题解:

The good pair is distance between leaves is <= threshold.

For the current root, we need to know for left and right subtree, the leaves distance and its corresponding count. distance : count.

Iterate both left and right, if distance sum <= threshold. Accumlate the product to the result.

Since returned array is used by one level up, thus when root is leaf, set resArr[1] as one, but not resArr[0] as one.

Time Complexity: O(n * distance ^ 2).

Space: O(n*distance). For the balanced tree, the leaf level has n / 2 nodes. Each node has distance array.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     int result = 0;
18     public int countPairs(TreeNode root, int distance) {
19         if(distance <= 1){
20             return result;
21         }
22 
23         dfs(root, distance);
24         return result;
25     }
26 
27     private int[] dfs(TreeNode root, int distance){
28         int[] resArr = new int[distance];
29         if(root == null){
30             return resArr;
31         }
32 
33         if(root.left == null && root.right == null){
34             resArr[1] = 1;
35             return resArr;
36         }
37 
38         int[] left = dfs(root.left, distance);
39         int[] right = dfs(root.right, distance);
40         for(int l = 1; l < left.length; l++){
41             for(int r = 1; r < right.length; r++){
42                 if(l + r <= distance){
43                     result += left[l] * right[r];
44                 }
45             }
46         }
47 
48         for(int i = 1; i < resArr.length; i++){
49             resArr[i] = left[i - 1] + right[i - 1];
50         }
51 
52         return resArr;
53     }
54 }

 

标签:distance,Pairs,Good,TreeNode,val,tree,good,Leaf,root
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18302856

相关文章

  • SP14887 GOODA - Good Travels 题解
    题目传送门前置知识Tarjan算法|最短路解法缩点后原图就成为了一个有向无环图,此时每个点最多被经过一次,故在求最长路的过程中可以将点权和边权混着转移。上篇题解用拓扑实现查找两点间最长路的做法正确性不会证,遂写了份Dijkstra求最长路。代码#include<bits/stdc++.h......
  • Uniapp 使用 Leaflet
    在Uniapp中使用Leaflet,可以按照以下步骤进行:安装Leaflet:如果您使用的是H5平台,可以通过以下命令在项目根目录安装Leaflet:npminstallleaflet对于其他平台(如小程序),可能无法直接通过npm安装,需要手动引入Leaflet的相关资源文件。在页面中引入Leaflet:在需......
  • layui js thymeleaf 公共工具类
    layuijsthymeleaf公共工具类其中功能包括:普通表格渲染树形表格渲染普通编辑(添加/删除/编辑)更多编辑(添加/编辑/更多)上传图片constcommon={getTable(table,url,cols,condition){if(!condition||condition==''){condition=......
  • CAD Exchanger SDK 3.24.0 终极版-say goodbye
    随着今年即将结束,还有一件重要的事情——CADExchanger3.24.0的发布。此次最新更新带来了一系列虽小但仍然值得注意的改进。其中包括ManufacturingToolkit(MTK)和Unity增强功能、Lab和VisualizationToolkit中模型部件检测的改进以及WebToolkit(WTK)中的修复。......
  • 基于springboot+layui+thymeleaf的学生成绩管理系统设计与实现(源码+SQL+使用说明)
    本项目适合做计算机相关专业的毕业设计,课程设计,技术难度适中、工作量比较充实。完整资源获取点击下载完整资源1、资源项目源码均已通过严格测试验证,保证能够正常运行;2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通;3、本项目比较适合计算......
  • YC311A [ 20240701 CQYC省选模拟赛 T1 ] 好串(good)
    题意给定一个长度为\(n\)的\(01\)串。定义一个串是好的当且仅当该串的所有前缀以及所有后缀的\(1\)的数量大于等于\(0\)的数量。你需要维护\(q\)个查询,每次求\(S_{l,...,r}\)的子串最少添加的\(1\)的个数使得该子串是好的。Sol首先不难发现一个正确的贪心,也......
  • vk-data-goods-sku-popup uniapp vue3示例
    效果图组件简介vk-data-goods-sku-popup是一个uniapp上面方便好用的sku组件,使用场景包括但不限于商品详情页、购物车页面、订单结算页、搜索结果页下面就上代码了,完整vue页面的代码如下<scriptsetup>import{ref,defineEmits,defineProps,computed}from'vue'//显示......
  • Leaflet-vue 热力图 (设置热力图颜色)
    使用的热力图是heatmap.js因为是Leaflet,所以还要引入一个对应的插件leaflet-heatmap.js,这个插件就在heatmap目录下的plugins里面。代码如下:import"heatmap.js";importHeatmapOverlayfrom"@/utils/leaflet-heatmap.js";letcfg={radius:0.5,//半径maxOpacity......
  • LaTeX 编辑协作平台 Overleaf 安装和使用教程
    在学术界和科技行业,LaTeX已成为撰写高质量文档的标准工具。然而,传统的LaTeX使用体验常常伴随着以下挑战:学习曲线陡峭环境配置复杂多人协作困难实时预览不便当然,市面上不乏很多在线LaTeX编辑平台,但它们大多是封闭的商业服务,无法完全满足用户对数据隐私和自主可控的需求......
  • LeetCode 2097. Valid Arrangement of Pairs
    原题链接在这里:https://leetcode.com/problems/valid-arrangement-of-pairs/description/题目:Youaregivena 0-indexed 2Dintegerarray pairs where pairs[i]=[starti,endi].Anarrangementof pairs is valid ifforeveryindex i where 1<=i<pairs.l......