You are viewing a single comment's thread. Return to all comments →
Thanks for this! Borrowed and did a little adjustments in Python. When I did it, I didn't distinguish between left and right nodes for the height. Even if there are both left and right nodes for root, the return value will always be 1 or 0 when passed back up:
add = 0
add = 1 + height(root.left)
add = 1 + height(root.right)
Even though your solution somehow passed all of the cases, I think your solution is incorrect. If you try input as: 4 3 4 2 1, your output will be 1, but the correct answer is 2.
To put it in other words, this solution will only return height of the right branch and override the calculations done on the left branch.