Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def fun(self, node, ans):
- if not node:
- return 0
- # leaf node
- if not node.left and not node.right:
- return node.data
- # subtree sums
- left_sum = self.fun(node.left, ans)
- right_sum = self.fun(node.right, ans)
- # Sum Tree condition
- if left_sum + right_sum != node.data:
- ans[0] = False
- # total subtree sum
- return left_sum + right_sum + node.data
- class Solution:
- def is_sum_tree(self, node):
- if not node:
- return True
- ans = [True]
- self.fun(node, ans)
- return ans[0]
Advertisement
Add Comment
Please, Sign In to add comment