Height of Binary Tree | GFG|
class Solution {
//Function to find the height of a binary tree.
int height(Node node)
{
// code here
if(node==null) return 0;
Stack<Node> q=new Stack<Node>();
q.add(node);
while(!q.isEmpty())
{
Node curr=q.peek();
{
if(curr.left!=null)
{
q.add(curr.left);
}
else if(curr.right!=null)
{
q.add(curr.right);
}
else
{
break;
}
}
}
int ans=q.size();
return ans;
}
Comments
Post a Comment