You are viewing a single comment's thread. Return to all comments →
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 {
/* class Node int data; Node left; Node right; */ public static int height(Node root) { // Write your code here. if(root ==null) return 0; Stack<Node> stk=new Stack<Node>(); stk.push(root); int res=-1; while(!stk.isEmpty()){ int size= stk.size(); for(int k=0;k<size;k++){ Node curr= stk.pop(); if(curr.left!=null){ stk.push(curr.left); } if(curr.right!=null){ stk.push(curr.right); } } res++; } return res; } 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; } }
do not know where I am going wrong with this code } scan.close(); int height = height(root); System.out.println(height); } }
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 →
import java.util.; import java.io.;
class Node { Node left; Node right; int data;
}
class Solution {
do not know where I am going wrong with this code } scan.close(); int height = height(root); System.out.println(height); }
}