Check if all Leaf Nodes are at same level in Binary tree
Question: Given a binary tree root node, check if all Leaf Nodes are at same level in Binary tree Solution: We can solve this question with the help of pre-order traversal. First we visit the left leaf and update the level value, and then check with the right leaf. If both are same we return […]
Check if binary tree is a height balanced tree?
Question: Given a binary tree root node, check if the tree is a height balanced tree. What is a height balanced tree? A tree is called as height balanced tree if the difference between left height and right height should not be more than 1. It means, the value should lie in between (-1, 0, […]
Display elements between any two given level
Question: Given a binary tree root node and 2 level, print the nodes between those levels. Example: Consider the image given below If given level is 2 and 3 the nodes that should be printed are: 10, 25, 7, 15, 18, 30 Solution: We do this with the help of level order traversal. While doing […]
Get Maximum width of Binary Tree
Question: Given a binary tree root node, get the maximum width of that tree Example: Consider the image given below Width at level 1 is 1. Because it has only 1 node. Width at level 2 is 2. Because it has only 2 nodes. Width at level 3 is 4. Because it has only 4 […]
Get Sum of elements Level wise in a Binary Tree
Question: Given a binary tree root node, get the level wise sum. Example: Consider the image given below Level 1 Sum = 16 Level 2 Sum = 10 + 25 = 35 Level 3 Sum = 7 + 15 + 18 + 30 = 70 Solution: While doing level order traversal, compute the sum at […]
Get Level of a Given Node
Question: Given a binary tree root node, get the level of the node. Example: Consider the image given below Image 1 If the given node is “25” level is 2 If the given node is “15” level is 3 Steps to get the level of a given node: 1. If the node is null, return […]
Delete a binary tree
Question: Given a binary tree root node, delete that tree Example: Consider the tree given below. How do you delete it? Here if you delete root node first, the you will loose link to left and right children. So we need to user post order traversal. In Post order traversal, we visit left, right and […]
Check if Two Trees are Mirror Structure to each other
Question: Given a 2 binary trees root nodes, check if the structure is mirror to each other. Example: Consider the below example: 1 1 /\ /\ / \ / \ / […]
Convert a binary tree to its Mirror Tree
Question: Given a binary tree root node, convert that tree to it’s mirror. Example: Consider the image given below and its mirror. Above image shows a tree and it’s mirror tree. From the above image we can see that, except the root node, all the other nodes have changed from right to left or left […]
Perform PostOrder traversal on a Binary Tree by without Recursion
Question: Given a binary tree root node, perform Post Order traversal by using stacks. Example: Consider the image given below: PostOrder Traversal (Left, Right, Root): 7 15 10 18 30 25 16 Now lets see how to perform Post Order Traversal: (Left, Right, Root) In Post-Order traversal, first Left node will be printed, then right […]