Last Updated: 19 Nov, 2021

Tree ordering

Easy
Asked in companies
UberAmazon

Problem statement

You were jogging in the morning and found a Binary Tree ‘T’ containing ‘N’ nodes numbered from ‘1’ to ‘N’. You have just learned the Pre-Order Traversal, so you quickly performed the Pre-Order Traversal on the Binary Tree, but you did not like the outcome. So you decided to flip a minimum number of the nodes so that the Pre-Order Traversal of the new Binary tree matches the array ‘A’. A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

Note: Any node in the binary tree can be flipped by swapping its left and right subtrees. For example, flipping node ‘1’ will have the following effect:

Input Format-
First-line contains ‘T’, denoting the number of Test cases.
For each Test case:
The first line contains an integer ‘N’, denoting the number of nodes in the BT.
The following line will contain the values of the tree’s nodes in the level order form ( -1 for 'NULL' node). Refer to the example for further clarification.
The next line contains 'N' space-separated integers denoting the array 'A'.

The input of the tree depicted in the image above will be like :

5 → represents the total number of nodes.
1 2 3 -1 -1 4 5 -1 -1 -1 -1 → represents the level order of Tree.
Explanation :
Level 1 :
The root node of the tree is 1.

Level 2 :
The left child of 1 = 2.
The right child of 1 = 3.

Level 3 :
The left child of 2 = null (-1)
The right child of 2 = null (-1)
The left child of 3 = 4
The right child of 3 = 5

Level 4:
The left child of 4 = null (-1)
The right child of 4 = null (-1)
The left child of 5 = null (-1)
The right child of 5 = null (-1)
Output Format-
For each test case, print all the nodes you want to flip. You can print the nodes in any order. The total number of nodes must be minimum. If it is impossible to flip the nodes in the tree to make the pre-order traversal match A, Print -1.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints -
1 <= ‘T’ <= 5
1 <= ‘N’ <= 10^5
Note- the sum of ‘N’ over all test cases does not exceed 10^5.

Time Limit: 1 sec

Approaches

01 Approach

Approach: We will do a Pre-Order Traversal on the tree and if the left child of the current node does not match with the node that we need then we flip at the current node.

 

Algorithm:

  • Func dfs(current, ans, A, index).
    • If the current node is ‘Null’.
      • Return.
    • If the current node is not equal to A[index].
      • Clear ‘ans’.
      • Append -1 to ‘ans’.
      • Return.
    • Increment ‘index’ by 1.
    • If the left child of the current node is not equal to A[index].
      • Append the current node to ‘ans’.
      • Call the func dfs(root -> right, ans, A, index).
      • Call the func dfs(root -> left, ans, A, index).
    • Else.
      • Call the func dfs(root -> left, ans, A, index).
      • Call the func dfs(root -> right, ans, A, index).
  • Func Main().
    • Create an empty vector ‘ans’.
    • Create a variable ‘index’ and initialize it to 0.
    • Call the functions dfs.
    • If the first value in ans is ‘-1’.
      • Print -1.
    • Else.
      • Print all the values of ‘ans’.