New update is available. Click here to update.

Last Updated: 11 Dec, 2020

Moderate

```
Height of a tree is the maximum number of nodes in a path from the node to the leaf node.
An empty tree is a height-balanced tree. A non-empty binary tree is a height-balanced binary tree if
1. The left subtree of a binary tree is already the height-balanced tree.
2. The right subtree of a binary tree is also the height-balanced tree.
3. The difference between heights of left subtree and right subtree must not more than β1β.
```

```
Input: Consider the binary tree given below:
```

```
Output: 'true'
Explanation:
Consider subtree at Node ( 7 )
Left subtree height is β0β and right subtree height is β0β, the absolute height difference is β0-0 = 0β and β0β is not more than β1β so subtree at Node ( 7 ) is a height-balanced binary tree.
Same for subtrees at Nodes ( 5, 6 ).
Consider subtree at Node ( 4 )
Left subtree height is β1β and right subtree height is β0β, the absolute height difference is β1-0 = 1β and β1β is not more than β1β so subtree at Node ( 4 ) is a height-balanced binary tree.
Same for subtree at Node ( 3)
Consider subtree at Node ( 2 )
Left subtree height is β2β and right subtree height is β1β, the absolute height difference is β2-1 = 1β and β1β is not more than β1β so subtree at Node ( 2 ) is a height-balanced binary tree.
Consider subtree at Node ( 1 )
Left subtree height is β3β and right subtree height is β2β, the absolute height difference is β3-2 = 1β and β1β is not more than β1β so subtree at Node ( 1 ) is a height-balanced binary tree.
Because the root node ( 1 ) is a height-balanced binary tree, so the complete tree is height-balanced.
```

```
The only line contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image will be:
```

```
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1).
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
```

```
Return 'true' or 'false' as stated in the problem statement.
```

```
You do not need to print anything; it has already been taken care of. Just implement the given function.
```

Approaches

The basic idea is that, check for every subtree the current subtree is height-balanced or not

If the subtree is height-balanced then check for its parent subtree. So we need to check the height of the current subtreeβs left and right and check the left subtree and right subtreeβs height-balanced.

- To implement this approach we need to make sure two thing
- The height difference of the left child subtree and the right child subtree must not more than β1β.
- Left child subtree and right child subtree must be already balanced.

- In height function -
- We implement a height function in which we return the height of a given node
- Suppose given node is βcurrentNodeβ than we first find the height of left child subtree height βcurrentNode->leftβ and right child subtree height βcurrentNode->rightβ
- Return max of (currentNode->left, currentNode-> right) +1 , we are extra β1β because β+1β denotes height of current Node in its both children subtrees.
- If βcurrentNodeβ has no left child and right child then return β1β.

- In βbalancedBinaryTreeβ method -
- In this function, we check if the given nodeβs subtree is height-balanced or not.
- To check the given subtreeβs height balancing -
- The first check height difference of both left child subtree and right child subtree height is not more than β1β and left subtree and right subtree both are height-balanced, to check height we use height function which we have already implemented
- If both conditions are true then return true else return false.

The basic idea is to compute height while calling a recursive function. To check if subtrees are balanced or not. So we do some changes in the previous approach, in which we were calling the height function for every node and at the returning time, we were returning βtrueβ if the current subtree is balanced otherwise returning βfalseβ. But in this approach, we return the height of the current subtree/node. if we have to return βtrueβ then we return the height of the current subtree/node and if we have to return βfalseβ then return β-1β.

- In βhelperMethodβ function -
- In this function, we check if the given nodeβs subtree is a height-balanced binary tree or not.
- To check if the given subtree is a height-balanced tree or not
- Firstly to check, if the current subtree/node βcurrentNodeβ is balanced, we call a recursive function for left subtree βcurrentNode->leftβ and right subtree βcurrentNode->rightβ and we get two integer values from βleftβ and βrightβ child which represent the height of left and right subtree. If the returned value is not β-1β then the respective subtree is balanced otherwise respective subtree is unbalanced.
- βleftValueβ = call recursive function for βcurrentNode->leftβ.
- βrightValueβ = call recursive function for βcurrentNode->rightβ.
- By these two recursive we get two integer values if one of them is β-1β, itβs mean either βleftβ subtree or βrightβ subtree is not balanced so respective subtree return β-1β as the height of subtree
- Then we return β-1β because if the child node/subtree is not balanced then the parent node canβt be balanced.

- If both values βleftValueβ and βrightValueβ are not β-1β and the absolute difference between them is not more than 1 then return a max of ( βleftValueβ, βrightValueβ )+1.

- The above step is done in βhelperMethodβ, in the main method, βhelperMethodβ will return an integer
- If the return value is not β-1β then return βTrueβ because the root node or tree is balanced.
- Otherwise, return βFalseβ.

Similar problems

Preorder Traversal

Easy

Posted: 18 Jan, 2022

Preorder Traversal

Easy

Posted: 18 Jan, 2022

Inorder Traversal

Easy

Posted: 19 Jan, 2022

Inorder Traversal

Easy

Posted: 19 Jan, 2022

Postorder Traversal

Easy

Posted: 20 Jan, 2022

Postorder Traversal

Easy

Posted: 20 Jan, 2022

Height of Binary Tree

Easy

Posted: 22 Apr, 2022

Locked Binary Tree

Easy

Posted: 12 May, 2022