2236. Root Equals Sum of Children LeetCode Solution
In this guide, you will get 2236. Root Equals Sum of Children LeetCode Solution with the best time and space complexity. The solution to Root Equals Sum of Children problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Root Equals Sum of Children solution in C++
- Root Equals Sum of Children solution in Java
- Root Equals Sum of Children solution in Python
- Additional Resources
Problem Statement of Root Equals Sum of Children
You are given the root of a binary tree that consists of exactly 3 nodes: the root, its left child, and its right child.
Return true if the value of the root is equal to the sum of the values of its two children, or false otherwise.
Example 1:
Input: root = [10,4,6]
Output: true
Explanation: The values of the root, its left child, and its right child are 10, 4, and 6, respectively.
10 is equal to 4 + 6, so we return true.
Example 2:
Input: root = [5,3,1]
Output: false
Explanation: The values of the root, its left child, and its right child are 5, 3, and 1, respectively.
5 is not equal to 3 + 1, so we return false.
Constraints:
The tree consists only of the root, its left child, and its right child.
-100 <= Node.val <= 100
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2236. Root Equals Sum of Children LeetCode Solution in C++
class Solution {
public:
bool checkTree(TreeNode* root) {
return root->val == root->left->val + root->right->val;
}
};
/* code provided by PROGIEZ */
2236. Root Equals Sum of Children LeetCode Solution in Java
class Solution {
public boolean checkTree(TreeNode root) {
return root.val == root.left.val + root.right.val;
}
}
// code provided by PROGIEZ
2236. Root Equals Sum of Children LeetCode Solution in Python
class Solution:
def checkTree(self, root: TreeNode | None) -> bool:
return root.val == root.left.val + root.right.val
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.