How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space

前端 未结 11 1120
借酒劲吻你
借酒劲吻你 2021-01-31 22:12

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.

11条回答
  •  终归单人心
    2021-01-31 22:40

    ***I am giving this solution in Java***
    
    import javafx.util.Pair;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Comparator;
    
    public class MinimumSwapRequiredBTintoBST {
        //Node of binary tree
        public static class Node{
            int data;
            Node left;
            Node right;
            public Node(int data){
                this.data = data;
                this.left = null;
                this.right = null;
            }
        }
        public static void main(String []arg){
            root = new Node(1);
            root.left = new Node(2);
            root.right = new Node(3);
            root.left.left = new Node(4);
            root.left.right = new Node(5);
            root.right.left = new Node(6);
            root.right.right = new Node(7);
            System.out.print("Tree traverasl i.e inorder traversal :");
            inorder(root);
            System.out.println(" ");
            MinimumSwapRequiredBTintoBST bst = new MinimumSwapRequiredBTintoBST();
            bst.convertBTBST(root);
        }
    
        private static void inorder(Node root) {
            if(root == null) return;
            inorder(root.left);
            System.out.print(root.data + "  ");
            inorder(root.right);
    
        }
    
       static Node root;
        int[] treeArray;
        int index = 0;
    
    // convert binary tree to binary search tree
        public void convertBTBST(Node node){
            int treeSize = elementsOfTree(node);
            treeArray = new int[treeSize];
            convertBtToArray(node);
            // Sort Array ,Count number of swap
    
            int minSwap = minimumswap(treeArray);
            System.out.println("Minmum swap required to form BT to BST :" +minSwap);
        }
    
        private static int minimumswap(int[] arr) {
            int n =arr.length;
            // Create two arrays and use as pairs where first
            // is element and secount array as position of first element
            ArrayList> arrpos =
                new ArrayList>();
            // Assign the value
            for(int i =0;i(arr[i],i));
            }
    // Sort the array by array element values to get right
    //position of every element as the elements of secound array
    
            arrpos.sort(new Comparator>() {
                @Override
                public int compare(Pair o1, Pair o2) {
                    return o1.getKey()-o2.getKey();
                }
            });
    // To keep track of visited elements .Initially all elements as not visited so put them as false
            int ans = 0;
            boolean []visited = new boolean[n];
            Arrays.fill(visited, false);
            // Traverse array elements
            for(int i =0;i0){
                    ans += cycle_size-1;
                }
            }
            return ans;
        }
    
        private void convertBtToArray(Node node) {
            // Check whether tree is empty or not.
            if (root == null) {
                System.out.println("Tree is empty:");
                return;
            }
        else{
            if(node.left != null) {
             convertBtToArray(node.left);}
                treeArray[index] = node.data;
                index++;
                if(node.right != null){
                    convertBtToArray(node.right);
                }
    
            }
        }
        private int elementsOfTree(Node node) {
            int height = 0;
            if(node == null) return 0;
            else{
                height = elementsOfTree(node.left )+ elementsOfTree(node.right)+1;
            }
            return height;
        }
    
    
    }
    

提交回复
热议问题