bkl_outatme

 avatar
bruteCoder
java
a year ago
703 B
4
Indexable


//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);
    }
}
Editor is loading...
Leave a Comment