Problem Description
Given a binary tree consisting of exactly 3 nodes — the root, its left child, and its right child — determine if the value of the root is equal to the sum of the values of its two children.
Key Insights
- The tree is very small with only 3 nodes.
- Directly access the left and right children of the root.
- Compare root.val with left.val + right.val.
- Handle negative values since node values can be between -100 and 100.
Space and Time Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Solution
Since the tree contains exactly 3 nodes, the solution is straightforward. Access the left and right child nodes of the root and sum their values. If the sum equals the root's value, return true; otherwise, return false. This solution uses a simple comparison without any need for recursion or iterative traversal, which is efficient given the fixed tree size.