返回介绍

solution / 1900-1999 / 1973.Count Nodes Equal to Sum of Descendants / README_EN

发布于 2024-06-17 01:03:12 字数 4896 浏览 0 评论 0 收藏 0

1973. Count Nodes Equal to Sum of Descendants

中文文档

Description

Given the root of a binary tree, return _the number of nodes where the value of the node is equal to the sum of the values of its descendants_.

A descendant of a node x is any node that is on the path from node x to some leaf node. The sum is considered to be 0 if the node has no descendants.

 

Example 1:

Input: root = [10,3,4,2,1]
Output: 2
Explanation:
For the node with value 10: The sum of its descendants is 3+4+2+1 = 10.
For the node with value 3: The sum of its descendants is 2+1 = 3.

Example 2:

Input: root = [2,3,null,2,null]
Output: 0
Explanation:
No node has a value that is equal to the sum of its descendants.

Example 3:

Input: root = [0]
Output: 1
For the node with value 0: The sum of its descendants is 0 since it has no descendants.

 

Constraints:

  • The number of nodes in the tree is in the range [1, 105].
  • 0 <= Node.val <= 105

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def equalToDescendants(self, root: Optional[TreeNode]) -> int:
    def dfs(root):
      if root is None:
        return 0
      l, r = dfs(root.left), dfs(root.right)
      if l + r == root.val:
        nonlocal ans
        ans += 1
      return root.val + l + r

    ans = 0
    dfs(root)
    return ans
/**
 * 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 {
  private int ans;

  public int equalToDescendants(TreeNode root) {
    dfs(root);
    return ans;
  }

  private int dfs(TreeNode root) {
    if (root == null) {
      return 0;
    }
    int l = dfs(root.left);
    int r = dfs(root.right);
    if (l + r == root.val) {
      ++ans;
    }
    return root.val + l + r;
  }
}
/**
 * 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:
  int equalToDescendants(TreeNode* root) {
    int ans = 0;
    function<long long(TreeNode*)> dfs = [&](TreeNode* root) -> long long {
      if (!root) {
        return 0;
      }
      auto l = dfs(root->left);
      auto r = dfs(root->right);
      ans += l + r == root->val;
      return root->val + l + r;
    };
    dfs(root);
    return ans;
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func equalToDescendants(root *TreeNode) (ans int) {
  var dfs func(*TreeNode) int
  dfs = func(root *TreeNode) int {
    if root == nil {
      return 0
    }
    l, r := dfs(root.Left), dfs(root.Right)
    if l+r == root.Val {
      ans++
    }
    return root.Val + l + r
  }
  dfs(root)
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文