Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
To solve the problem, we must first distinguish leaves from internal nodes.
For some node v, if v.left == null or v.right == null, then v is not a leaf.
If the previous condition does not hold, and v.left.right == v, then v is a leaf node.
Now that we can check if some node is a leaf, we can make the function “heightOfTree” a recursive function that returns the tree's height in which its input is the root of that subtree.