Category Archives: Trees

Depth of the deepest odd level node in Binary Tree

Given a Binary tree, find out depth of the deepest odd level leaf node. Take root level as depth 1. Examples: Input : Output : 5 We can traverse the tree starting from the root level and keep curr_level of the node. Increment the curr_level each time we go to left or a right subtree.… Read More »

Calculate depth of a full Binary tree from Preorder

Given preorder of a binary tree, calculate its depth(or height) [starting from depth 0]. The preorder is given as a string with two possible characters. ‘l’ denotes the leaf ‘n’ denotes internal node The given tree can be seen as a full binary tree where every node has 0 or two children. The two children… Read More »

Print all nodes in a binary tree having K leaves

Given a binary tree and a integer value K, the task is to find all nodes in given binary tree having K leaves in subtree rooted with them. Examples: // For above binary tree Input : k = 2 Output: {5, 7} // here node 5 and 7 have k = 2 leaves Input :… Read More »

Find maximum level sum in Binary Tree

Given a Binary Tree having positive and negative nodes, the task is to find maximum sum level in it. Examples: Input : 4 / \ 2 -5 / \ /\ -1 3 -2 6 Output: 6 Explanation : Sum of all nodes of 0’th level is 4 Sum of all nodes of 1’th level is… Read More »

Continuous Tree

A tree is Continuous tree if in each root to leaf path, absolute difference between keys of two adjacent is 1. We are given a binary tree, we need to check if tree is continuous or not. Examples: Input : 3 / \ 2 4 / \ \ 1 3 5 Output: “Yes” // 3->2->1… Read More »