Doubt regarding Balanced Binary Tree

bool isBalanced(TreeNode root) {
if (root == null) {
return true;
}
int left = depth(root.left);
int right = depth(root.right);
if (left - right > 1 || left - right < -1)
{
return false;
}
else
{
return isBalanced(root.left) && isBalanced(root.right); // can’t we write else { return false; } and not doing this dfs becauz we are already getting the height of the tree
}
}
}

int depth(TreeNode n) {
if (n == null) {
return 0;
}
if (n.left == null && n.right == null) {
return 1;
}
else {
int left = depth(n.left);
int right = depth(n.right);
return 1 + (left > right ? left : right);
}

Hi @ritwikbiswas
Please share your complete code using ide.codingblocks.com .

Hi Ritwik,
Since you are not responding to this thread , I am marking your doubt as resolved for now. You may reopen this thread if your doubt still persists.

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.