java - Binary Search Tree : Insertion -


i working on exercise binary search tree : insertion on hackerrank. here problem statement:

you given pointer root of binary search tree , value inserted tree. insert value appropriate position in binary search tree , return root of updated binary tree. have complete function.

i submitted following solution , passed 4 of 6 test cases , failed 2 of 6 test cases problem not able see 2 test cases failed not sure why failing. have tried create own test cases , seemed working correctly. can think of test cases solution not work for? hoping point me in right direction

 /* node defined : class node  int data; node left; node right;  */  static node insert(node root,int value) {    if (root == null){        root = new node();        root.data = value;        return root;    }    arraylist<node> list = new arraylist<node>();    list.add(root);    getnode(list,value);    return root; }  static void getnode(arraylist<node> list,int value){     arraylist<node> newlist = new arraylist<node>();     (int i=0;i<list.size();i++){         newlist.add(list.get(i).left);         newlist.add(list.get(i).right);         if(list.get(i).left == null){             list.get(i).left = new node();             list.get(i).left.data = value;             return;         }         if(list.get(i).right == null){             list.get(i).right = new node();             list.get(i).right.data = value;             return;         }     }     getnode(newlist,value); } 

static node insert(node root,int value) {

   if(root == null)    {        node new_node = new node();        new_node.data=value;        root = new_node;    }  if(value < root.data)     {     if(root.left != null)         {         node.insert(root.left,value);     }     else         {         node new_node = new node();         new_node.data = value;         root.left=new_node;     }  } if(value > root.data)     {     if(root.right !=null)         {          node.insert(root.right,value);     }     else         {         node new_node = new node();         new_node.data=value;         root.right=new_node;     }  } return root; } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -