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.
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.
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β.
Preorder Traversal
Preorder Traversal
Inorder Traversal
Inorder Traversal
Postorder Traversal
Postorder Traversal
Height of Binary Tree
Locked Binary Tree