Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Node {
  5. Node left;
  6. Node right;
  7. int data;
  8.  
  9. Node(int data) {
  10. this.data = data;
  11. left = null;
  12. right = null;
  13. }
  14. }
  15.  
  16. class Solution {
  17.  
  18. /*
  19. class Node
  20. int data;
  21. Node left;
  22. Node right;
  23. */
  24. static int height=0;
  25. public static int height(Node root) {
  26. getHeight(root,0);
  27. return height;
  28. }
  29.  
  30. public static void getHeight(Node root,int h){
  31. if(root!=null){
  32. if(root.left==null&&root.right==null){
  33. updateHeight(h);
  34. }
  35. else{
  36. getHeight(root.left,h+1);
  37. getHeight(root.right,h+1);
  38. }
  39. }
  40. }
  41.  
  42.  
  43. static void updateHeight(int h){
  44. if(h>height){
  45. height=h;
  46. }
  47. }
  48. public static Node insert(Node root, int data) {
  49. if(root == null) {
  50. return new Node(data);
  51. } else {
  52. Node cur;
  53. if(data <= root.data) {
  54. cur = insert(root.left, data);
  55. root.left = cur;
  56. } else {
  57. cur = insert(root.right, data);
  58. root.right = cur;
  59. }
  60. return root;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement