Advertisement
porteno

Untitled

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