Find the height of the binary tree given that only the nodes on the even levels are considered as the valid leaf nodes.
The height of a binary tree is the number of edges between the tree’s root and its furthest leaf. But what if we bring a twist and change the definition of a leaf node. Let us define a valid leaf node as the node that has no children and is at an even level (considering root node as an odd level node).
Output :Height of tree is 4
Solution : The approach to this problem is slightly different from the normal height finding approach. In the return step, we check if the node is a valid root node or not. If it is valid, return 1, else we return 0. Now in the recursive step- if the left and the right sub-tree both yield 0, the current node yields 0 too, because in that case there is no path from current node to a valid leaf node. But in case at least one of the values returned by the children is non-zero, it means the leaf node on that path is a valid leaf node, and hence that path can contribute to the final result, so we return max of the values returned + 1 for the current node.
Height of tree is 4
Time Complexity:O(n) where n is number of nodes in given binary tree.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Print middle level of perfect binary tree without finding height
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Check if all leaves are at same level
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Find the maximum path sum between two leaves of a binary tree
- Find sum of all left leaves in a given Binary Tree
- Print all nodes in a binary tree having K leaves
- Find sum of all right leaves in a given Binary Tree
- Minimum sum path between two leaves of a binary tree
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Check if a given Binary Tree is height balanced like a Red-Black Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.