Check if two Binary Trees are Isomorphic
Question: Given a binary tree root node, check if the tree is a isomorphic tree. 2 trees are called isomorphic to each other, if they have same structure or mirror structure. And their data should also match. Solution in C++ #include <iostream> #include <queue> #include <stack> using namespace std; // structure to hold binary tree […]
Check if Binary Tree is Foldable Tree
Question: Given a binary tree root node, check if the tree is a foldable tree. Example: Consider the image given below We can see that, the nodes of left and right Subtree are exact mirror of each other. So we can solve this problem by using “Check if Two Trees are Mirror Structure to each […]
Find Maximum or Minimum in Binary Tree
Problem Statement: Given a binary tree root node, you need to find the minimum and maximum value of the nodes. Consider the image below: Minimum value is 7 and maximum value is 30. Solution: We can solve this problem by traversing the tree and checking the value with min and max variables. Initially assign minimum […]
Get Sum of all Leaf Nodes in Binary Tree
Problem Statement: Given a binary tree root node, you need to find the sum of all the leaf nodes. Consider the image below: The leaf nodes are 7, 15, 18, 30. So the sum is 70. We can solve this problem by checking if the node is a leaf node or not. If it is […]
Get Average of all nodes in Binary Tree
Question: Given a binary tree root node, get the average of all nodes in Binary Tree Solution: The sum of all the nodes is 121 Total number of nodes are :7 So the average is 121/7 = 17.2 Solution in C++ #include <iostream> #include <queue> #include <stack> using namespace std; // structure to hold binary […]
Get Sum of all nodes formed from Root to Leaf Path
Question: Given a binary tree root node, get the sum of the values of all the nodes formed from root to the leaf node. Solution: From the above image, we have 4 leaf nodes i.e : 3, 4, 6, 7 So the paths will be 1 -> 2 -> 3 Number = 123 1 -> […]
Check if given two nodes are siblings to each other in Binary Tree
Question: Given a binary tree root node and 2 node value, check if those 2 nodes are siblings. Solution: If we send the node value as 7 and 15, they are siblings If we send the node value as 7 and 18 they are not siblings. So the solution here is, for every node it […]
Find Sibling node of a given node value in Binary Tree
Question: Given a binary tree root node and a node value, get the sibling of that node. Solution: If the input node value is 10, its sibling is 25 If the input node value is 7, its sibling is 15 If the input node value is 16, its sibling is NULL We can solve this […]
Check if given Binary Tree is BST
Question: Given a binary tree root node, check if it is a Binary Search Tree Solution: A Binary Tree is a BST if it satisfy below 2 conditions. The node values left of the root node should always be less and The node values right of the root node should always be higher than the […]
Find Parent of a given node value in Binary Tree
Question: Given a binary tree node and root node, get the parent node of that node. Solution: Solution is very simple. Before we go to the child node of any parent node, we check if the children nodes is equal to given value. If it is equal, then we return that node. Else we recursively […]