We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
staticclassNode{Nodeleft;Noderight;intdata;publicNode(){}publicNode(intdata){this.data=data;}}publicstaticNodeinsert(Noderoot,intdata){Noden=newNode(data);Nodehead=root;if(root==null){root=n;}while(head!=null){if(data<head.data&&head.left==null){head.left=n;break;}elseif(data<head.data&&head.left!=null){head=head.left;}elseif(data>=head.data&&head.right!=null){head=head.right;}elseif(data>=head.data&&head.right==null){head.right=n;break;}}returnroot;}publicstaticvoidmain(String[]args)throwsIOException{/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));// Not really used, this is the num of elements to be insertedintsize=Integer.parseInt(bufferedReader.readLine().trim());List<Integer>tree=Stream.of(bufferedReader.readLine().replaceAll("\\s+$","").split(" ")).map(Integer::parseInt).collect(toList());Noderoot=insert(null,tree.get(0));for(inti=1;i<tree.size();i++){root=insert(root,tree.get(i));}/* printPreOrder(root, bufferedWriter); bufferedWriter.newLine(); */// Here is where the logic must be placed...intheight=getHeight(root);bufferedWriter.write(String.valueOf(height));bufferedReader.close();bufferedWriter.close();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Height of a Binary Tree
You are viewing a single comment's thread. Return to all comments →
For Java 15