Loading...
Loading...
You are given the root of a binary tree, root. Return the sum of the values of every leaf that is a right child.
A leaf is a node with no children. The root is never a right child, even when it has no children itself, so the root never counts.
Return 0 if there are no right leaves.
Your function receives root as a TreeNode. The node type is provided for you, with val, left, and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
root is in the range [1,105].Node.val ≤104Only 7 is a leaf that is a right child (the right child of 20); 9 is a left leaf and 15 is a left leaf, so neither counts. The answer is 7.
3 is a right leaf (the right child of the root) and 5 is a right leaf (the right child of 2); 4 is a left leaf, so it does not count. The answer is 3 + 5 = 8.
The single node 1 has no right child at all, so there are no right leaves and the answer is 0.