Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Q1 {
- public static int numNodes (BinNode <Integer>bt){
- if(bt==null){
- return 0;
- }
- return numNodes(bt.getLeft())+numNodes(bt.getRight())+1;
- }
- public static int sumNodes(BinNode<Integer>bt)
- {
- if(bt==null)
- return 0;
- return sumNodes(bt.getLeft())+sumNodes(bt.getRight())+ bt.getValue();
- }
- public static int numPositive(BinNode<Integer>bt)
- {
- if(bt==null)
- return 0;
- int out =0;
- if(bt.getValue()>0)
- out =1;
- return numPositive(bt.getLeft())+numPositive(bt.getRight())+out;
- }
- public static int maxInTree (BinNode <Integer> bt){
- if (bt ==null)
- return 0;
- int maxLeft=maxInTree(bt.getLeft());
- int maxRight=maxInTree(bt.getRight());
- int bigger=Math.max(maxLeft,maxRight);
- return Math.max(bigger,bt.getValue());
- }
- public static void main(String[] args) {
- public static boolean isExist(BinNode <Integer> bt,int num1){
- if (bt.getValue==null)
- return 0;
- return isExist()
- }
- BinNode<Integer> bt1= new BinNode <Integer>(3);
- bt1.setRight(new BinNode<Integer>(6));
- bt1.setLeft(new BinNode<Integer>(10));
- BinNode pos=bt1.getLeft();
- pos.setRight(new BinNode<Integer>(4));
- pos.setLeft(new BinNode<Integer>(-2));
- pos=bt1.getRight();
- pos.setRight(new BinNode<Integer>(9));
- pos.setLeft(new BinNode<Integer>(6));
- System.out.println(numNodes(bt1));
- System.out.println(numPositive(bt1));
- System.out.println(sumNodes(bt1));
- System.out.println(maxInTree(bt1));
- }
- public static boolean isLeaf(BinNode<Integer>bt)
- {
- if(bt.getLeft()==null&&bt.getRight()==null)
- {
- return true ;
- }
- return false;
- }
- public static countLeaves(BinNode<Integer>bt)
- {
- if(bt==null)
- return 0;
- int count=0;
- if(bt.getRight()==null&&0 )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement