Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- */
- static int height=0;
- public static int height(Node root) {
- getHeight(root,0);
- return height;
- }
- public static void getHeight(Node root,int h){
- if(root!=null){
- if(root.left==null&&root.right==null){
- updateHeight(h);
- }
- else{
- getHeight(root.left,h+1);
- getHeight(root.right,h+1);
- }
- }
- }
- static void updateHeight(int h){
- if(h>height){
- height=h;
- }
- }
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement