Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean isBalanced() {
- return checkHeight(this.root) == -1 ? false : true;
- }
- private int checkHeight(TreeNode node) {
- if(node == null){
- return 0;
- }
- int leftHeight = checkHeight(node.leftChild);
- if (leftHeight == -1) {
- return -1;
- }
- int rightHeight = checkHeight(node.rightChild);
- if (rightHeight == -1) {
- return -1;
- }
- if (Math.abs(leftHeight - rightHeight) > 1) {
- return -1;
- } else {
- return Math.max(leftHeight, rightHeight)+1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement