Check if binary tree is a sum tree
In this tutorial we shall check if the tree is a sum tree or not. Problem Statement: You are given the root node of a binary tree. You need to check if that tree is a sum tree or not. Example: Consider the binary tree given below: In the above image, it is a sum […]
Check if 2 binary tree are identical
In this chapter, we shall see if 2 trees are identical. Problem statement: You are given root node of 2 binary trees, you need to check if 2 trees are identical. Example: Consider the below image The first set are identical; the second set are not identical. From the image we can derive below true […]
Check if 2 nodes are mirror of each other
In this tutorial we shall see how to check if 2 trees are mirror to each other. Problem Statement: You are given root node of 2 trees. You need to check if those 2 trees are mirror to each other. For example: Below 2 trees are mirror to each other. As you can see above, […]
Lowest Common ancestor of a Binary Tree.
In this chapter we shall see how to calculate lowest common ancestor in a binary tree. Problem Statement: You are given root node of a tree along with 2 nodes. You need to calculate the lowest common ancestor. Ancestor is the common node between 2 nodes. Hence you are given 2 nodes, you need to […]
Diameter of a Binary Tree
In this chapter we shall solve how to find out diameter of a binary tree. Problem statement: You are given root node of a binary tree, you need to find the diameter in that tree. Now wait a minute, Diameter term is usually used in reference to a circle, but how is it possible to […]
Spiral order or Zigzag traversal of a Binary Tree
In this chapter we shall see tree spiral order traversal using 2 stacks. Problem statement: Given a root of the Binary tree, you need to print the nodes in a spiral order. Consider the tree as below: The zigzag order will be as shown below: The solution will be A, c, b, d, e, f, […]
Height or Max depth if a BTree
In this chapter we shall calculate the height of a Binary Tree. Problem Statement: Given the root node of binary tree, calculate the height of that tree. Example: Consider the tree below: The height of the binary tree will be 4. The path will be: a -> b -> e -> h First we shall […]
Vertical Order Traversal
In this tutorial we shall see how to do vertical order traversal. Problem Statement: You are given a root node of a tree, you need to print the nodes when they are traversed vertically. Consider the tree given below: The vertical order traversal can be visualized as below: So the vertical order traversal will be: […]
Level Order Traversal
In this tutorial we shall see how to print level by level of the tree nodes. Problem Statement: Given the root node of the tree, print the nodes level by level. For example: Consider the tree below: Here the level order traversal will be as below: [a] [b, c] [d, e, f, g] i.e we […]
Left view and right view of a Binary Tree
Problem Statement: Given a binary tree, print the left and right view of the tree. For example: If you have a tree as below: The left view will be a, b, d, h and right view will be a, c, g, j. As highlighted in image below. So how do we solve this problem? We […]