Java: Minimum Swaps required to convert binary tree to binary search tree

Level : Intermediate
Mentor: Shailendra Chauhan
Type : GuidedLab
Points : 10
Duration : 00:30:00

Lab Details

Problem Statement:

Given the array representation of Complete Binary Tree i.e, if index i is the parent, index 2*i + 1

is the left child and index 2*i + 2 is the right child. The task is to find the minimum number of

swap required to convert it into Binary Search Tree.

Input:

arr[] = {5, 6, 7, 8, 9, 10, 11}

Output:

Output: 3

Swap 1: Swap node 8 with node 5.

Swap 2: Swap node 9 with node 10.

Swap 3: Swap node 10 with node 7.

So, Minimum 3 Swaps are required

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this