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
- Construct Ancestor Matrix from a Given Binary Tree
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- K-th ancestor of a node in Binary Tree
- K-th ancestor of a node in Binary Tree | Set 3
- Kth ancestor of a node in binary tree | Set 2
- Lowest Common Ancestor in a Binary Tree | Set 1
- 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 Search 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 unique matrix n x n for an input n
- Construct a linked list from 2D matrix
- Construct a linked list from 2D matrix (Iterative Approach)
- Construct a Binary Search Tree from given postorder
Improved By : ManasChhabra2