zoukankan      html  css  js  c++  java
  • 1530. Number of Good Leaf Nodes Pairs

    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].
    

    Example 4:

    Input: root = [100], distance = 1
    Output: 0
    

    Example 5:

    Input: root = [1,1,1], distance = 2
    Output: 1
    

    Constraints:

    • The number of nodes in the tree is in the range [1, 2^10].
    • Each node's value is between [1, 100].
    • 1 <= distance <= 10
    class Solution {
    private int res;
        
        public int countPairs(TreeNode root, int distance) {
    
            res = 0;
            helper(root, distance);
            return res;
        }
    
        private int[] helper(TreeNode node, int distance) {
    
            if (node == null) return new int[11];
    
            int[] left = helper(node.left, distance);
            int[] right = helper(node.right, distance);
    
            int[] A = new int[11];
    
            // node is leaf node, no child, just return
            if (node.left == null && node.right == null) {
                A[1] = 1;
                return A;
            }
    
            // find all nodes satisfying distance
            for (int i = 0; i <= 10; ++i) {
                for (int j = 0; j <= 10; ++j) {
                    if (i + j <= distance) res += (left[i] * right[j]);
                }
            }
    
            // increment all by 1, ignore the node distance larger than 10
            for (int i = 0; i <= 9; ++i) {
                A[i + 1] += left[i];
                A[i + 1] += right[i];
            }
    
            return A;
        }
    }

    https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/755784/Java-Detailed-Explanation-Post-Order-Cache-in-Array

  • 相关阅读:
    物料外部编码的模糊查询
    ◆◆1LSMW的简单范例-LSMW导入会计科目
    通过Tcode查找LSMW
    LSMW批量更新物料主数据
    ◆◆0HR报表开发-Join & Projection
    HR常用宏(一)
    ◆◆0PM01-创建信息类型(infotype)教程
    keras_9_激活函数 Activations
    keras_8_优化器 Optimizers
    keras_7_评估标准 Metrics
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13383935.html
Copyright © 2011-2022 走看看