Question

CODE IN JAVA** V. Given a pointer to the root of a binary tree and a...

CODE IN JAVA**
V. Given a pointer to the root of a binary tree and a pointer ‘p’ to a given node in the tree and a second pointer ‘q’ to another node in the tree write a routine which will return the total number of nodes in the tree that are on level ‘p’ and ‘q’. If they are on the same level you should multiply the answer by 3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// To find the level of a given node a
   public static int level(Node head,Node a,int level)
   {
       if(head==null)
           return 0;
       if(a==head)
           return level;
       int temp=level(head.left,a,level+1); // find the node in left subtree
       if(temp!=0) // if node is present in left subtree return the level else recure for right subtree
           return temp;
       return level(head.right,a,level+1);
      
   }
  
   public static int countNodeAtLevelA(Node head ,int a,int level)
   {
       if(head==null||a<0)
           return 0;
       int count=0;
       if(a==level) // if the current level matches with the required level
       {
           count+=1;
          
       }
       count+=countNodeAtLevelA(head.left,a,level+1); // recur for left subtree
       count+=countNodeAtLevelA(head.right,a,level+1); // recur for right subtree
       return count; // return the total count
   }
  
   public static int countNode(Node root,Node p,Node q)
   {
       int level1=level(root,p,0); // find the level of node p
       int level2=level(root,q,0); // find the level of node q
       int count1=countNodeAtLevelA(root,level1,0); // count the node at level a
       if(level1==level2)
           return count1*3;
       count1+=countNodeAtLevelA(root,level2,0); // count the node at level b
       return count1;
   }
  

Add a comment
Know the answer?
Add Answer to:
CODE IN JAVA** V. Given a pointer to the root of a binary tree and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java please thanks! Given a pointer to a binary tree write a routine which will...

    in java please thanks! Given a pointer to a binary tree write a routine which will traverse the tree and return the number of nodes in the tree whose information field which is an integer is between 59 and 112 In addition return the value of the node with the largest integer and as well return a count of the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and...

  • java please IV. Given a pointer to a binary tree write a routine which will traverse...

    java please IV. Given a pointer to a binary tree write a routine which will traverse the tree and return the 59 and 112 number of nodes in the tree whose information field which is an integer is between In addition return the value of the node with the largest integer and as well return a count or the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and left...

  • in javascrift please Given a pointer to the root of a binary tree write a routine...

    in javascrift please Given a pointer to the root of a binary tree write a routine that will delete every node in the tree that is currently a leaf (has no sons), and when finished tell you how many nodes were deleted as well as how many nodes are left in the tree.

  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • Write a Java function named smallcount that, given the pointer to the root of a Binary...

    Write a Java function named smallcount that, given the pointer to the root of a Binary Search Tree (BST) and a key K, returns the number of nodes having key values less than or equal to K. Function smallcount should visit as few nodes in the BST as possible.

  • Given a pointer to a binary tree write a routine which will traverse the tree and...

    Given a pointer to a binary tree write a routine which will traverse the tree and return number of nodes in the tree who have just a right son or just a left son (do not include nodes have both right and left sons as well as do not include any nodes that do not have any sons at all).

  • The code should be in java. Question: In an infinite binary tree where every node has...

    The code should be in java. Question: In an infinite binary tree where every node has two children, the nodes are labelled in row order. In each row, the labeling is left to right. Given the label of a node in this tree, please write a method to return the labels in the path from the root of the tree to the node with that label. Your algorithm should have time complexity of O ( log ⁡ n ). Example...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • A collection of nodes is arranged as a binary search tree ordered on the field INFO which contain...

    A collection of nodes is arranged as a binary search tree ordered on the field INFO which contains distinct positive integers for each of the nodes. In addition to INFO, LLINK and RLINK, each node has three other fields CLASS SUCC and PRED CLASS is an information field containing a single letter that denotes the class to which the node belongs (there being up to 26 classes). The nodes in each class are arranged as a doubly-linked circular list with...

  • In Java: Given the following binary tree class, write a recursive method called size() which will...

    In Java: Given the following binary tree class, write a recursive method called size() which will find the number of nodes in the subtree rooted at the current node: class myBinaryTree{ int myValue; myBinaryTree left; myBinaryTree right; myBinaryTree(int inValue) {myValue = inValue;} public int size(){ <CODE WRITTEN HERE> } }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT