bkl_outatme

 avatar
bruteCoder
java
2 months ago
703 B
1
Indexable
Never


//User function Template for Java
class Solution {
    public Node flattenBST(Node root) {
        // Code here
        ArrayList<Node> list = new ArrayList<>();
        inorder(root,list);
        
        Node head = new Node(-1);
        Node dummy = head;
        
       for(Node el : list) {
           
           el.left = null;
           el.right = null;
           dummy.right = el;
           
           dummy = dummy.right;
          
       }
       
       return head.right;
    }
    
    public void inorder(Node root, ArrayList<Node> list){
        if(root == null) return;
        
        inorder(root.left,list);
        list.add(root);
        inorder(root.right,list);
    }
}
Leave a Comment