[
tree
dfs
bfs
recursion
]
BinarySearch 0181 Sum Tree
Problem statement
https://binarysearch.com/problems/Sum-Tree/
Solution
Just traverse tree with recursion.
Complexity
It is O(n)
for time and O(h)
for space.
Code
class Solution:
def solve(self, root):
if not root: return True
if not root.left and not root.right: return True
sm = 0
if root.left: sm += root.left.val
if root.right: sm += root.right.val
return self.solve(root.left) and self.solve(root.right) and sm == root.val