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.
Solution in Java
-->Using Level Order traversal in Java
publicstaticintheight(Noderoot){// Write your code here.if(root==null){return0;}Queue<Node>queue=newLinkedList<Node>();queue.add(root);intheight=0;while(true){intnodeCount=queue.size();if(nodeCount==0){returnheight-1;}height++;while(nodeCount>0){NodepresentNode=queue.remove();if(presentNode.left!=null){queue.add(presentNode.left);}if(presentNode.right!=null){queue.add(presentNode.right);}nodeCount--;}}}
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 →
Solution in Java -->Using Level Order traversal in Java