can you explain this because as much as i understood from question that much i write code but it’s not passing all the test cases.
Code issue , , , , , , , , , , , , , , ,, , , ,, , , ,
and this is my code***************************************************************************************public boolean isBalanced() { return this.isBalanced(this.root).isBalanced; } private BalancedPair isBalanced(Node node) { BalancedPair obj = new BalancedPair(); boolean n = check(node.left); boolean m=check1(node.right); if(n==true&&m==true) { obj.isBalanced=n; } else { obj.isBalanced=false; } return obj; } boolean check(Node root) { if(root==null) { return true; } if(root.left!=null&&root.right==null) { return false; } if(root.right!=null&&root.left==null) { return false; } boolean n=check(root.left); boolean m=check(root.right); if(n==true&&m==true) { return true; } else { return false; } } boolean check1(Node root) { if(root==null) { return true; } if(root.left!=null&&root.right==null) { return false; } if(root.right!=null&&root.left==null) { return false; } boolean n=check(root.left); boolean m=check(root.right); if(n==true&&m==true) { return true; } else { return false; }
@Nitin-Mishra-2380486738834604
The question says to find out whether a given binary tree is balanced or not. A non-empty binary tree T is balanced if:
- Left subtree of T is balanced
- Right subtree of T is balanced
- The difference between heights of left subtree and right subtree is not more than 1.
Also, an empty tree is balanced.
Algorithm:
- Get the height of left and right subtrees.
- Return true if difference between heights is not more than 1 and left and right subtrees are balanced, otherwise return false.
you have not calculated depth of subtree at any level
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.