Advertisement
davegimo

Untitled

Nov 16th, 2020
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1.   public static int conta(s A) {
  2.        
  3.         int conta = 0;
  4.         boolean f = checkUnival(A,A.val);
  5.         if (f) {
  6.             conta++
  7.         }
  8.         if (A.left != null) {
  9.             conta += conta(A.left);
  10.         }
  11.  
  12.         if (A.right != null) {
  13.             conta += conta(A.right);
  14.         }
  15.  
  16.  
  17.         return conta;
  18.     }
  19.  
  20.     public static boolean checkUnival(s A, int i) {
  21.        
  22.         if (A.left == null && A.right == null && A.val == i) {
  23.             return true;
  24.         }
  25.  
  26.         if (A.val == i) {
  27.             boolean checkLeft = true;
  28.             boolean checkRight = true;
  29.  
  30.             if (A.left != null) {
  31.                 checkLeft = checkUnival(A.left, i);
  32.             }
  33.    
  34.             if (A.right != null) {
  35.                 checkLeft = checkUnival(A.left, i);
  36.             }
  37.            
  38.             return checkLeft && checkRight;
  39.         }
  40.         else {
  41.             return false;
  42.         }
  43.  
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement