Advertisement
porteno

Untitled

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