Untitled
unknown
plain_text
10 months ago
708 B
4
Indexable
private boolean checkNode(TreeNode node) {
// Base case: If the node is null or a leaf, it satisfies the property
if (node == null || (node.left == null && node.right == null)) {
return true;
}
// Get the sum of child nodes (0 if child is null)
int leftVal = (node.left != null) ? node.left.val : 0;
int rightVal = (node.right != null) ? node.right.val : 0;
// Check if the current node's value equals the sum of its children
if (node.val != leftVal + rightVal) {
return false;
}
// Recursively check for left and right subtrees
return checkNode(node.left) && checkNode(node.right);
}
Editor is loading...
Leave a Comment