DDSA
Advertisement

2236. Root Equals Sum of Children

Time: O(1)
Space: O(1)

Approach

Check root.val == left.val + right.val directly.

2236.cs
C#
// Approach: Check root.val == left.val + right.val directly.
// Time: O(1) Space: O(1)

public class TreeNode
{
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
    {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}


public class Solution
{
    public bool CheckTree(TreeNode root)
    {
        return root.val == (root.left.val + root.right.val);
    }
}
Advertisement
Was this solution helpful?