top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find the max weight in the binary tree?

+2 votes
339 views

Given a binary tree where each node has some weight. You have to return the max weight in the binary tree.

posted Jul 12, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

declare an integer MAX and assign any negative number to the variable MAX , for example MAX=-1
traverse the given binary tree by using any of the (Preorder,Postorder,Inorder,Level order) available traversing techniques, if weight of any node is greater then MAX , assign weight of that node to MAX. here is the Preorder travesrsal function that returns Max as maximum weight of binary tree

int preorderTraversal(root){
   if(root){
      if(MAX<root->weight)
         MAX=root->weight;
      preorderTraversal(root->left);
      preoderTraversal(root->right);
   }
  return MAX;
}
answer Jul 27, 2016 by Shahsikant Dwivedi
Similar Questions
0 votes

How to find longest consecutive path in a binary tree using C/C++/Java?

0 votes

Given the root of a Binary Tree along with two integer values. Assume that both integers are present in the tree.
Find the LCA (Least Common Ancestor) of the two nodes with values of the given integers.
2 pass solution is easy. You must solve this in a single pass.

+2 votes

Say you are given a tree and weight is calculated by sum of values in the nodes in that path.

...