# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up