# Category Archives: Trees

View algorithms on Trees

## Find the left view of a binary tree.

Question: Given the root pointer to a binary tree, find the left view of the tree. Input: Sample Tree (Pointer to node 1 is given). Output: 1, 2, 4 At a single glance of the problem, we can understand that we need to perform a level order traversal on the tree. This is similar to finding the number… Read More »

## How many binary trees are possible with n nodes?

Question: How many binary trees are possible with n nodes? Input: Nodes = 3 Output: Answer = 5 For, example consider a tree with 3 nodes (n = 3), it will have a maximum combination of 5 different trees In general, if there are n nodes, there exist (2n)!/(n+1)! different trees.

## Give an algorithm to find the LCA(Least Common Ancestor) of two nodes in a Binary tree.

Question: Given the root pointer to a binary tree, find the LCA (Least Common Ancestor) of two nodes in the tree. Input: Sample Tree (Pointer to node 1 is given). Find LCA of node 5 and 4 Output: Answer = 3 A simple recursive approach can be used to find the LCA (Least common ancestor).

## Given two binary trees, determine if they are mirror trees.

Question: Given 2 binary trees, determine if they are mirror of each other Input: 2 tree pointers are given. (Root pointer) Output: TRUE We can again apply the recursive sub-structure property of the tree here. The algorithm can be implemented as:- Time Complexity:- O(n) Space Complexity:- O(n) due to a recursive stack

## Given a binary tree, find the sum of all its elements without using recursion.

Question: Given the root pointer to a binary tree, find the sum of all its elements without using recursion. Input: Sample Tree (Pointer to node 1 is given). Output: Sum = 15 We discussed how to find the sum of all elements of a binary tree using recursion in this post. Given a binary tree, find the sum… Read More »

## Given a binary tree, find the sum of all its elements.

Question: Given the root pointer to a binary tree, find the sum of all its elements. Input: Sample Tree (Pointer to node 1 is given). Output: Sum = 15 This is a very simple problem. Since the tree follows a recursive sub-structure, we can recursively call left tree sum, right tree sum and then add their values to… Read More »

## Given a binary tree, construct its mirror tree.

Question: Given a binary tree, find its mirror tree. Input: A tree pointer is given. (Root pointer) Output: Pointer of mirrored tree. We can easily get the mirrored tree by a simple recursive algorithm. We traverse the tree and swap the left and right pointers. Time Complexity:- O(n) Space Complexity:- O(n) (due to recursive stack)

## Given two binary trees, find if they are structurally identical.

Question: Given two binary trees, find if they are structurally identical. Input: 2 trees are given. (Root pointers) Output: TRUE When we say structurally identical trees, we mean that both the trees have exactly the same number of nodes, arranged in the exactly same way. It is not necessary that the value of each node should also be… Read More »

## Find the height of the binary tree without using recursion.

Question: Given the root pointer to a binary tree, find the height. Input: Sample Tree (Pointer to node 1 is given). Output: 3 We discussed the recursive method to find the height of the binary tree in this post- Find the height of the binary tree The non-recursive method will definitely require the level order traversal technique. The… Read More »

## Give an algorithm to find the number of full nodes in a binary tree.

Question: Given the root pointer to a binary tree, find the number of full nodes. Input: Sample Tree (Pointer to node 1 is given). Find the number of full nodes. Output: Number of full nodes = 3 According to the definition, the set of all nodes with both left and right child are called as full nodes. In… Read More »