In this code when binary tree is null then get height -1 but actual height is 0.
This code is not working for empty binary tree .i want to know my doubt right or wrong if right then solve it
public int height() {
return this.height(this.root);
}
private int height(Node root) {
if(root==null) {
return -1;
}
return Math.max(height(root.left),height(root.right))+1;
}
// this code from binary tree height lecture
if root is null return 0
when I return 0 then rest of answer wrong .
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.