Advertisement
davegimo

Untitled

Nov 16th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public class s {
  2.  
  3. public int val;
  4. public s left;
  5. public s right;
  6.  
  7.  
  8. public s(int val, s left, s right) {
  9. this.val = val;
  10. this.left = left;
  11. this.right = right;
  12. }
  13.  
  14. public s(int val) {
  15. this.val = val;
  16. }
  17.  
  18.  
  19. public static int conta(s A) {
  20.  
  21. int conta = 0;
  22. boolean f = checkUnival(A,A.val);
  23. if (f) {
  24. conta++;
  25. }
  26. if (A.left != null) {
  27. conta += conta(A.left);
  28. }
  29.  
  30. if (A.right != null) {
  31. conta += conta(A.right);
  32. }
  33.  
  34.  
  35. return conta;
  36. }
  37.  
  38. public static boolean checkUnival(s A, int i) {
  39.  
  40. if (A.left == null && A.right == null && A.val == i) {
  41. return true;
  42. }
  43.  
  44. if (A.val == i) {
  45. boolean checkLeft = true;
  46. boolean checkRight = true;
  47.  
  48. if (A.left != null) {
  49. checkLeft = checkUnival(A.left, i);
  50. }
  51.  
  52. if (A.right != null) {
  53. checkLeft = checkUnival(A.left, i);
  54. }
  55.  
  56. return checkLeft && checkRight;
  57. }
  58. else {
  59. return false;
  60. }
  61.  
  62. }
  63.  
  64.  
  65.  
  66. public static void main(String[] args) {
  67.  
  68. s A = new s(0);
  69. s B = new s(1);
  70. s C = new s(1);
  71.  
  72. // s D = new s(1);
  73. // s E = new s(1);
  74. // s F = new s(1);
  75. // s G = new s(1);
  76. // s H = new s(0);
  77.  
  78. A.left = B;
  79. A.right = C;
  80. // C.left = D;
  81. // D.left = E;
  82. // D.right = F;
  83.  
  84.  
  85. System.out.println(conta(A));
  86.  
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement