The maximum distance between nodes in a binary tree (the longest path of the tree)-recursive method, the maximum distance of a binary tree

Source: Internet
Author: User

The maximum distance between nodes in a binary tree (the longest path of the tree)-recursive method, the maximum distance of a binary tree

In the previous article, we described a kind of deformation of this question and provided a non-recursive solution.

Now I will giveOriginal questionARecursive Solution. As you can see, the Code Implementation of the recursive solution provided in the previous blog is quite concise.


Problem description:

If we regard a binary tree as a graph, and the line between parent and child nodes is bidirectional, we would like to define "distance" as the number of edges between two nodes.

Write a program to find the distance between the two farthest nodes in a binary tree. Tree for testing:

N1

/\

N2 n3

/\

N4 n5

/\/\

N6 n7 n8 Environment

//

N10 n11

Algorithm:

In the previous blog, we used the depth of the tree. In the recursive solution to this problem, I found that the height of the tree is much easier than that of the depth.

This is because: For a leaf node, the height of its sub-tree (although not) can be considered as 0, and its own height is 1, which is easy to distinguish. If the depth is used, it will be 0, which will lead to some tedious judgment.

The question is to find the longest path in a tree.

For node t, the longest path of the tree with it as the root must be one of the threeMaximum Value:

The longest path of the Left subtree of ① t lpath

② The longest path of the right subtree of t rpath

③ Height of the Left subtree of t + height of the right subtree of t

-- Conclusion 1

Code implementation:

To be concise and elegant, I tried to simplify the code as much as possible. I may have sacrificed some ease of coding and added some operations (such as the long string of return statements forcibly spelled out ..)

It is worth noting that The code sequence cannot be changed.Because the premise for assigning values to t-> floor is that the height of the left and right subtree of t is known, they are obtained by the first two rows of recursive code. Therefore, the order cannot be changed !!

Node:

// Node struct BinaryTreeNode {BinaryTreeNode * left = NULL; BinaryTreeNode * right = NULL; int floor = 1 ;};

Key code:

// Find the maximum path. The returned path length is int FindMaxPath (BinaryTreeNode * t) {if (t) {int lpath = FindMaxPath (t-> left ); // maximum path of the Left subtree int rpath = FindMaxPath (t-> right ); // maximum path of the right subtree // the number of layers of the tree with t root is equal to the maximum number of layers of the subtree + 1 t-> floor = max2 (t-> left )? T-> left-> floor: 0, (t-> right )? T-> right-> floor: 0) + 1; // Conclusion 1 return max3 (lpath, rpath, (t-> left )? T-> left-> floor: 0) + (t-> right )? T-> right-> floor: 0);} return 0 ;}


Use recursive algorithms to delete Binary Tree nodes

Void tree: Delete_Tree (node * p)
{
If (! (P-> lChild )&&! (P-> rChild ))
Cout <endl <p-> dat; // indicates deleting a node.
Else
If (p-> lChild)
Delete_Tree (p-> lChild );
Else
Delete_Tree (p-> rChild );

Cout <endl <p-> dat; // indicates deleting a node. Delete the node and try again.
}

The number of nodes is certain. The longest binary tree in the tree path is ______, and the shortest binary tree in the tree path is ____

Regular Binary Tree, full Binary Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.