Root equals sum of children
December 17, 2022
treeProblem URL: Root equals sum of children
We will check the value of root with the sum of the rest of the tree. If the value of root is equal to the sum of the rest of the tree, then we will return True
. Otherwise, we will return False
.
# 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 checkTree(self, root: Optional[TreeNode]) -> bool:
return root.val == root.left.val + root.right.val
Time complexity: O(1)
Space complexity: O(1)