Source : https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height :
Function Description
Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer.
getHeight or height has the following parameter(s):
- root: a reference to the root of a binary tree.
Note -The Height of binary tree with single node is taken as zero.
Input Format
The first line contains an integer , the number of nodes in the tree.
Next line contains space separated integer where th integer denotes node[i].data.
Note: Node values are inserted into a binary search tree before a reference to the tree's root node is passed to your function. In a binary search tree, all nodes on the left branch of a node are less than the node value. All values on the right branch are greater than the node value.
Constraints
Output Format
Your function should return a single integer denoting the height of the binary tree.
Sample Input
Sample Output
3
Explanation
The longest root-to-leaf path is shown below:
There are nodes in this path that are connected by edges, meaning our binary tree's .
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); | |
} | |
} |
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
ReplyDeleteHello 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
ReplyDeleteOBCTOP 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
ReplyDeleteIts good as your other content : D, thankyou for posting . Water Tanks
ReplyDeleteAllianz 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
ReplyDeleteListcry 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
ReplyDeleteIts good as your other content thank you for posting. Logo Design
ReplyDeleteYT1s 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.
ReplyDeleteDazzlingway Your Way To Fame And Stardom
ReplyDeleteThere 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_/
ReplyDeleteWatch 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. ดูหนังออนไลน์
ReplyDeleteThere 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/
ReplyDeleteI like this web blog very much, Its a really nice place to read and get info . 토토사이트
ReplyDeleteWe 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
ReplyDeleteThis 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. 꽁머니사이트
ReplyDeleteAs the name suggests, you might think leatherjacketcollection leather jackets are only our priority in terms of quality and price.
ReplyDeletehttps://leatherjacketcollection.com/
Colossis provides the best stable diffusion image search on the web. Give it a try today! royalty free images
ReplyDeleteNext, 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
ReplyDeleteI 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.
ReplyDeleteThis 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
ReplyDeletehttps://www.40billion.com/profile/169617022
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
ReplyDeleteI 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