---
# System prepended metadata

title: 1973. Count Nodes Equal to Sum of Descendants
tags: [Leetcode, DFS, Medium]

---

# 1973. Count Nodes Equal to Sum of Descendants
###### tags: `Leetcode` `Medium` `DFS`
Link: https://leetcode.com/problems/count-nodes-equal-to-sum-of-descendants/
## 思路
简单dfs
## Code
```java=
class Solution {
    int ans = 0;
    public int equalToDescendants(TreeNode root) {
        dfs(root);
        return ans;
    }
    public int dfs(TreeNode root){
        if(root == null) return 0;
        int leftSum = dfs(root.left);
        int rightSum = dfs(root.right);
        if(leftSum+rightSum==root.val){
            ans++;
        }
        return leftSum+rightSum+root.val;
    }
}
```