Advertisement
porteno

Untitled

Mar 14th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1.  
  2. public class Q3 {
  3.  
  4. public static void main(String[] args) {
  5. BinNode<Integer> t1 = build_tree_ok();
  6. BinNode<Integer> t2 = build_tree_ok();
  7.  
  8. //boolean res = onePath(t1);
  9. boolean res = grisha(t1);
  10. System.out.println(res);
  11.  
  12. }
  13.  
  14.  
  15. public static boolean grisha(BinNode<Integer>t) {
  16. if(t==null)
  17. return false;
  18. if(t.getLeft()==null && t.getRight()==null)
  19. return true;
  20. if(t.getLeft()!=null && t.getRight()!=null) {
  21. if(t.getRight().getValue()!=t.getValue() &&
  22. t.getLeft().getValue() != t.getValue())
  23. return false;
  24. if(t.getLeft().getValue() == t.getValue() &&
  25. t.getRight().getValue()!=t.getValue())
  26. return grisha(t.getLeft());
  27. if(t.getRight().getValue() == t.getValue() &&
  28. t.getRight().getValue()!=t.getValue())
  29. return grisha(t.getRight());
  30. return grisha(t.getLeft())||grisha(t.getRight());
  31.  
  32. }
  33. if(t.getRight()!=null && t.getLeft()!=null)
  34. if(t.getLeft().getValue()==t.getValue())
  35. return grisha(t.getLeft());
  36. if(t.getLeft()!=null && t.getRight()!=null)
  37. if(t.getRight().getValue()==t.getValue())
  38. return grisha(t.getRight());
  39. return false;
  40. }
  41. public static boolean isLeaf(BinNode<Integer> t) {
  42. if(t == null)
  43. return false;
  44. if(t.getLeft() == null && t.getRight() == null)
  45. return true;
  46. return false;
  47. }
  48. //correct solution part1
  49. private static boolean onePath(BinNode<Integer> t1) {
  50. boolean r = onePath2(t1, t1.getValue());
  51. return r;
  52. }
  53. //correct solution part2
  54. private static boolean onePath2(BinNode<Integer> t, int x) {
  55. if(t==null)
  56. return false;
  57. int val = t.getValue();
  58. if(val!=x)
  59. return false;
  60. if(isLeaf(t))
  61. return val==x;
  62. return onePath2(t.getLeft(), val) || onePath2(t.getRight(), val);
  63.  
  64. }
  65.  
  66. public static BinNode<Integer> build_tree_ok() {
  67.  
  68. BinNode<Integer> nd1 = new BinNode<Integer>(1);
  69. nd1.setLeft(new BinNode<Integer>(1));
  70.  
  71. BinNode<Integer> nd2 = new BinNode<Integer>(1);
  72. nd2.setLeft(new BinNode<Integer>(1));
  73. nd2.setRight(new BinNode<Integer>(2));
  74. nd2.getLeft().setRight(nd1);
  75.  
  76. BinNode<Integer> nd3 = new BinNode<Integer>(1);
  77. nd3.setLeft(new BinNode<Integer>(1));
  78. nd3.setRight(new BinNode<Integer>(2));
  79.  
  80. BinNode<Integer> nd4 = new BinNode<Integer>(1);
  81. nd4.setRight(new BinNode<Integer>(2));
  82. nd4.getRight().setRight(new BinNode<Integer>(1));
  83. nd4.setLeft(nd3);
  84.  
  85. BinNode<Integer> root = new BinNode<Integer>(2);
  86. root.setRight(nd2);
  87. root.setLeft(nd4);
  88.  
  89. return root;
  90.  
  91. }
  92. public static BinNode<Integer> build_tree_notOk() {
  93. /*
  94. * BinNode<Integer> nd1 = new BinNode<Integer>(1); nd1.setLeft(new
  95. * BinNode<Integer>(1));
  96. *
  97. * BinNode<Integer> nd2 = new BinNode<Integer>(1); nd2.setLeft(new
  98. * BinNode<Integer>(1)); nd2.setRight(new BinNode<Integer>(2));
  99. * nd2.getLeft().setRight(nd1);
  100. *
  101. * BinNode<Integer> nd3 = new BinNode<Integer>(1); nd3.setLeft(new
  102. * BinNode<Integer>(1)); nd3.setRight(new BinNode<Integer>(2));
  103. *
  104. * BinNode<Integer> nd4 = new BinNode<Integer>(1); nd4.setRight(new
  105. * BinNode<Integer>(2)); nd4.getRight().setRight(new BinNode<Integer>(1));
  106. * nd4.setLeft(nd3);
  107. */
  108. BinNode<Integer> root = new BinNode<Integer>(2);
  109. /*
  110. * root.setRight(nd2); root.setLeft(nd4);
  111. */
  112. return root;
  113.  
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement