Given an ancestor matrix mat[n][n] where Ancestor matrix is defined as below.
mat[i][j] = 1 if i is ancestor of j mat[i][j] = 0, otherwise
Construct a Binary Tree from given ancestor matrix where all its values of nodes are from 0 to n-1.
- It may be assumed that the input provided the program is valid and tree can be constructed out of it.
- Many Binary trees can be constructed from one input. The program will construct any one of them.
Input: 0 1 1 0 0 0 0 0 0 Output: Root of one of the below trees. 0 0 / \ OR / \ 1 2 2 1 Input: 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 Output: Root of one of the below trees. 5 5 5 / \ / \ / \ 1 2 OR 2 1 OR 1 2 OR .... / \ / / / \ / \ / 0 4 3 3 0 4 4 0 3 There are different possible outputs because ancestor matrix doesn't store that which child is left and which is right.
This problem is mainly reverse of below problem.
We strongly recommend you to minimize your browser and try this yourself first.
Observations used in the solution:
- The rows that correspond to leaves have all 0’s
- The row that corresponds to root has maximum number of 1’s.
- Count of 1’s in i’th row indicates number of descendants of node i.
The idea is to construct the tree in bottom up manner.
1) Create an array of node pointers node.
2) Store row numbers that correspond to a given count. We have used multimap for this purpose.
3) Process all entries of multimap from smallest count to largest (Note that entries in map and multimap can be traversed in sorted order). Do following for every entry.
…….a) Create a new node for current row number.
…….b) If this node is not a leaf node, consider all those descendants of it whose parent is not set, make current node as its parent.
4) The last processed node (node with maximum sum) is root of tree.
Below is C++ implementation of above idea. Following are steps.
Inorder traversal of tree is 0 1 4 5 3 2
Note that we can also use an array of vectors in place of multimap. We have used multimap for simplicity. Array of vectors would improve performance as inserting and accessing elements would take O(1) time.
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Construct Ancestor Matrix from a Given Binary Tree
- Construct Binary Tree from Ancestor Matrix | Top Down Approach
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Kth ancestor of a node in binary tree | Set 2
- K-th ancestor of a node in Binary Tree | Set 3
- K-th ancestor of a node in Binary Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Query for ancestor-descendant relationship in a tree
- Lowest Common Ancestor in a Binary Tree | Set 3 (Using RMQ)
- Lowest Common Ancestor in a Binary Tree | Set 1
- Lowest Common Ancestor in a Binary Search Tree.
- Lowest Common Ancestor for a Set of Nodes in a Rooted Tree
- Kth ancestor of a node in an N-ary tree using Binary Lifting Technique
- Least Common Ancestor of any number of nodes in Binary Tree
- Maximum difference between node and its ancestor in Binary Tree
- Lowest Common Ancestor in a Binary Tree | Set 2 (Using Parent Pointer)
- Construct a linked list from 2D matrix
- Construct a unique matrix n x n for an input n
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Construct a Matrix of size NxN with values in range [1, N^2] as per given conditions
Improved By : ManasChhabra2