Tree: Height of a Binary Tree HackerRank Solution


Tree: Height of a Binary Tree HackerRank Solution
Source : https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree



Source : https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree


Solution


// Karthikalapati.blogspot.com
import java.util.*;
import java.io.*;
class Node {
Node left;
Node right;
int data;
Node(int data) {
this.data = data;
left = null;
right = null;
}
}
class Solution {
/* We define a 1-Node tree to have height of 0 */
static int height(Node root) {
if (root == null) {
return -1;
} else {
return 1 + Math.max(height(root.left), height(root.right));
}
}
public static Node insert(Node root, int data) {
if (root == null) {
return new Node(data);
} else {
Node cur;
if (data <= root.data) {
cur = insert(root.left, data);
root.left = cur;
} else {
cur = insert(root.right, data);
root.right = cur;
}
return root;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
Node root = null;
while (t-- > 0) {
int data = scan.nextInt();
root = insert(root, data);
}
scan.close();
int height = height(root);
System.out.println(height);
}
}

22 comments:

  1. When most people think of a roof, they only think of the shingles. A roofing system, on the other hand, is far more complicated. Roofs may appear to be simple, but they are actually quite difficult. There are numerous layers of materials to install, and they must be done in the correct order. can an architect be an interior designer

    ReplyDelete
  2. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. SBOBET88

    ReplyDelete
  3. OBCTOP adalah situs judi slot online resmi terpercaya yang juga merupakan salah satu agen slot resmi di Indonesia yang menyediakan permainan slot online yang lengkap dengan berbagai jenis provider ternama di dunia seperti pragmaticplay, habanero dan pgsoft. slot online

    ReplyDelete
  4. Its good as your other content : D, thankyou for posting . Water Tanks

    ReplyDelete
  5. Allianz Packers and Movers in Hyderabad are highly skilled and experienced professional in packing and moving in Hyderabad and shifting household items securely ... Packers and Movers Delhi to Gurgaon

    ReplyDelete
  6. Listcry provide world class services Packers and Movers, Vehicle shifting, Household shifting, car transportation, office shifting, Local home Shifting in a ... Packers and Movers Noida to Lucknow

    ReplyDelete
  7. Its good as your other content thank you for posting. Logo Design

    ReplyDelete
  8. YT1s You can download YouTube videos to your device with the use of a YouTube downloader. Downloads are offered in a selection of formats and quality levels. A helpful website for finding user-uploaded YouTube videos is YT1s. It was originally released in 2020 and has a sizable, varied, and international user base.

    ReplyDelete
  9. Dazzlingway Your Way To Fame And Stardom

    ReplyDelete
  10.  There are also countless books on the subject, and most business schools offer courses in business planning. https://startupo.fr/question/1439/comment_se_mettre_en_auto_entrepreneur_/

    ReplyDelete
  11. Watch movies online for free, full HD, new movies, hit the theaters, Thai subtitles, watch movies on mobile phones for both iOS and Andriod, clear, smooth, without interruption, Full HD master, watch movies on TV, new movies hit the theaters click there. ดูหนังออนไลน์

    ReplyDelete
  12. There are many companies offering dissertation writing services. Read our article to know how to choose the best. dissertation-zone.com/pay-someone-to-write-my-dissertation/

    ReplyDelete
  13. I like this web blog very much, Its a really nice place to read and get info . 토토사이트

    ReplyDelete
  14. We prefer your present post. It may be excellent to see anybody clarify within phrases with the cardio together with lucidity with this crucial concern may be rapidly observed. read more

    ReplyDelete
  15. This is exactly likewise a terrific print which we honestly dearly loved looking through. It's not actually day by day that we include the risk to figure out an item. 꽁머니사이트

    ReplyDelete
  16. As the name suggests, you might think leatherjacketcollection leather jackets are only our priority in terms of quality and price.
    https://leatherjacketcollection.com/

    ReplyDelete
  17. Colossis provides the best stable diffusion image search on the web. Give it a try today! royalty free images

    ReplyDelete
  18. Next, in the payment gateway, you need to select PAYBIS and follow the instructions. If PAYBIS does not work in your country, then XPAY offers two more options for paying by card. vfxAlert PRO

    ReplyDelete
  19. I wanted some information about binary tree. Then I came to know about this post from google. I got very important information from this post. I got a lot of benefit from this information. This concept is explained very well here. Here https://www.biologysap.com/ I got the link of this site. The users of this site are in lakhs. This site's reviews are the best ever.

    ReplyDelete
  20. This is exactly likewise a terrific print which we honestly dearly loved looking through. It's not actually day by day that we include the risk to figure out an item
    https://www.40billion.com/profile/169617022

    ReplyDelete
  21. I'd prefer necessary . articles or blog posts, Produce beloved, We would desire considerably more facts by using, considering it is especially beautiful., Bless people intended for publishing. Intian e-viisumikelpoisuus Brasilian kansalaiset

    ReplyDelete
  22. I really like people unique writing. Will probably be quality to uncover just one reveal with text on the center and in some cases purity in this particular unique vital area of interest will likely be conveniently encountered. unesco heritage sites india for tourist visa

    ReplyDelete