Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1.  
  2.     public static void q1 (BinNode<Integer> bt)
  3.     {
  4.         if(bt == null)
  5.             return;
  6.  
  7.         if(bt.getValue()%2==0)
  8.             bt.setValue(bt.getValue()/2);
  9.  
  10.         q1(bt.getLeft());
  11.         q1(bt.getRight());
  12.     }
  13.  
  14.     public static void q2 (BinNode<Integer>bt)
  15.     {
  16.  
  17.         if(bt == null)
  18.             return;
  19.  
  20.         if( (!(bt.hasLeft())) && bt.hasRight())
  21.             bt.setLeft(new BinNode<Integer>(0));
  22.  
  23.         if( (!(bt.hasRight())) && bt.hasLeft())
  24.             bt.setRight(new BinNode<Integer>(0));
  25.  
  26.         q2(bt.getLeft());
  27.         q2(bt.getRight());
  28.     }
  29.  
  30.     public static int q3 (BinNode<Integer>bt)
  31.     {
  32.         if(bt == null)
  33.             return 0;
  34.  
  35.         if(bt.getValue()%2==0)
  36.             return 1 + q3(bt.getLeft()) + q3(bt.getRight());
  37.  
  38.         return q3(bt.getLeft()) + q3(bt.getRight());
  39.     }
  40.  
  41.     public static int q4 (BinNode<Integer>bt)
  42.     {
  43.         if(bt == null)
  44.             return 0;
  45.  
  46.         if(bt.hasRight())
  47.             return bt.getRight().getValue() + q4(bt.getLeft()) + q4(bt.getRight());
  48.  
  49.         return q4(bt.getLeft());
  50.     }
  51.  
  52.     public static boolean q5 (BinNode<Character>bt,char x)
  53.     {
  54.         if(bt == null)
  55.             return false;
  56.  
  57.         if(bt.getValue() == x)
  58.             return true;
  59.  
  60.         return q5(bt.getLeft(),x) || q5(bt.getRight(),x);
  61.     }
  62.  
  63.     public static boolean q6 (BinNode<Integer>bt)
  64.     {
  65.         if(bt == null)
  66.             return true;
  67.  
  68.         if(bt.getValue()%2!=0)
  69.             return false;
  70.  
  71.         return q6(bt.getLeft()) && q6(bt.getRight());
  72.     }
  73.  
  74.     public static int q7(BinNode<Integer>bt)
  75.     {
  76.         if(bt==null)
  77.             return 0;
  78.         return bt.getValue() + Math.max(q7(bt.getLeft()),q7(bt.getRight()));
  79.     }
  80.  
  81.     public static boolean ezer(BinNode<Integer>bt,int sum,int treesum)
  82.     {
  83.         if(bt==null)
  84.             return false;
  85.  
  86.         treesum += bt.getValue();
  87.  
  88.         if( treesum == sum && (!(bt.hasLeft())) && (!(bt.hasRight())) )
  89.             return true;
  90.  
  91.         return ezer(bt.getLeft(),sum,treesum) || ezer(bt.getRight(),sum,treesum);
  92.     }
  93.  
  94.     public static boolean rootsum(BinNode<Integer>bt,int sum)
  95.     {
  96.         return ezer(bt,sum,0);
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement