简体   繁体   中英

Path of the diameter of a binary tree

I have a binary tree and a method for the size of the longest path (the diameter):

int diameter(struct node * tree)
{

   if (tree == 0)
     return 0;

  int lheight = height(tree->left);
  int rheight = height(tree->right);

  int ldiameter = diameter(tree->left);
  int rdiameter = diameter(tree->right);

  return max(lheight + rheight + 1, max(ldiameter, rdiameter));
} 

I want the function to return also the exact path (list of all the nodes of the diameter). How can I do it?

Thanks

You have two options: A) Think. B) Search. Among the first few google hits you can find this: http://login2win.blogspot.hu/2012/07/print-longest-path-in-binary-tree.html

Choose A) if you want to learn, choose B) if you do not care, only want a quick, albeit not necessarily perfect solution.

There are many possible solutions, some of them:

  1. In a divide and conquer approach you will probably end up with maintaining the so far longest paths on both sides, and keep only the longer.
  2. The quoted solution does two traversals, one for determining the diameter, and the second for printing. This is a nice trick to overcome the problem of not knowing whether we are at the deepest point in approach 1.
  3. Instead of a depth first search, do a breadth first one. Use a queue. Proceed level by level, for each node storing the parent. When you reach the last level (no children added to queue), you can print the whole path easily, because the last printed node is on (one) longest path, and you have the parent links.

Add a property struct node * next to the node struct. Before the return statement, add a line like this tree->next = (ldiameter > rdiameter ? tree->left : tree->right) to get the longer path node as the next node. After calling diameter(root) , you should be able to iterate through all of the next nodes from the root to print the largest path.

I think the following may work... compute the diameter as follows in O(N) time.

// this is a c++ code
int findDiameter(node *root, int &max_length, node* &max_dia_node, int parent[], node* parent_of_root){
    if(!root) return 0;
    parent[root->val] = parent_of_root->val;
    int left = findDiameter(root->left, max_length);
    int right = findDiameter(root->right, max_length);
    if(left+right+1 > max_length){
        max_dia_node = root;
        max_length = left+right+1;
    }
    return 1 + max(left,right);
}

So in this function number of things is happening. First max_length is calculating the max diameter of the tree. And along with that I am assigning the max_dia_node to this node.

This is the node through which I will have my max diameter pass through.

Now using this information we can find the max depth left child and right child of this node (max_dia_node). From that we can have the actual nodes via "parent" array.

This is two traversal of the tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM