Microsoft and other data structures and algorithms interview 100 questions 11th questions

Source: Internet
Author: User

11th questions
Question:
Calculate the maximum distance between nodes in a binary tree...
If we regard a binary tree as a graph, and the line between parent and child nodes is bidirectional,
Let's just define "distance" as the number of edges between two nodes.
Write a program,
Calculate the distance between the two nodes with the farthest distance in a binary tree.

 
Analysis:
For this question, there are two cases: one is that there is no root node in the node with the maximum length, and the other is that there is a root node.
How can we solve the maximum distance between nodes in the tree? --> To solve the depth of the Left subtree of each node and the depth of the right subtree, you can solve the depth of the left and right subtree of each node, then, the sum of left and right depths is used as the maximum length,
Then, update the maximum length.
For Figure A, the depth of the Left subtree of the root node is 3, and the depth of the right subtree is 3, so the maximum length is 6,
For Figure B, set the left son node of the root node to T, the depth of the Left subtree of T to 2, and the depth of the right subtree to 2, so the maximum length is 4.
Therefore, we only need to solve the left subtree depth and right subtree depth of each node, and then update the maximum length continuously.

Code: Build A binary tree
[Cpp]
# Include <iostream>
Using namespace std;
 
Struct nodeBTree
{
NodeBTree * nodeBTreeLeft;
NodeBTree * nodeBTreeRight;
Int maxDepthLeft;
Int maxDepthRight;
Int index;
};
 
Int maxNum (int comp_a, int comp_ B)
{
If (comp_a> comp_ B)
Return comp_a;
Else
Return comp_ B;
}
Void updateMaxDepthLR (nodeBTree * BTreeHead, int & MaxLength)
{
Int maxDepthLC = 0;
Int maxDepthRC = 0;
If (NULL = BTreeHead) {return ;}
If (NULL! = BTreeHead-> nodeBTreeLeft)
{
UpdateMaxDepthLR (BTreeHead-> nodeBTreeLeft, MaxLength );
MaxDepthLC = max (BTreeHead-> nodeBTreeLeft-> maxDepthLeft, BTreeHead-> nodeBTreeLeft-> maxDepthRight );
BTreeHead-> maxDepthLeft = maxDepthLC + 1;
}
Else
{
BTreeHead-> maxDepthLeft = 0;
}
If (NULL! = BTreeHead-> nodeBTreeRight)
{
UpdateMaxDepthLR (BTreeHead-> nodeBTreeRight, MaxLength );
MaxDepthRC = max (BTreeHead-> nodeBTreeRight-> maxDepthLeft, BTreeHead-> nodeBTreeRight-> maxDepthRight );
BTreeHead-> maxDepthRight = maxDepthRC + 1;
}
Else
{
BTreeHead-> maxDepthRight = 0;
}
If (BTreeHead-> maxDepthRight + BTreeHead-> maxDepthLeft> MaxLength)
MaxLength = BTreeHead-> maxDepthRight + BTreeHead-> maxDepthLeft;
}
Int main ()
{
# Pragma region construct the binary tree // figure
NodeBTree * a = new struct nodeBTree ();
A-> index = 0;
NodeBTree * B = new struct nodeBTree ();
B-> index = 1;
NodeBTree * c = new struct nodeBTree ();
C-> index = 2;
NodeBTree * d = new struct nodeBTree ();
D-> index = 3;
NodeBTree * e = new struct nodeBTree ();
E-> index = 4;
NodeBTree * f = new struct nodeBTree ();
F-> index = 5;
NodeBTree * g = new struct nodeBTree ();
G-> index = 6;
NodeBTree * h = new struct nodeBTree ();
H-> index = 7;
NodeBTree * I = new struct nodeBTree ();
I-> index = 8;
 
A-> nodeBTreeLeft = B;
A-> nodeBTreeRight = c;
B-> nodeBTreeLeft = d;
B-> nodeBTreeRight = e;
C-> nodeBTreeLeft = f;
C-> nodeBTreeRight = g;
D-> nodeBTreeLeft = h;
D-> nodeBTreeRight = NULL;
E-> nodeBTreeLeft = NULL;
E-> nodeBTreeRight = NULL;
F-> nodeBTreeLeft = NULL;
F-> nodeBTreeRight = I;
G-> nodeBTreeLeft = NULL;
G-> nodeBTreeRight = NULL;
H-> nodeBTreeLeft = NULL;
H-> nodeBTreeRight = NULL;
I-> nodeBTreeLeft = NULL;
I-> nodeBTreeRight = NULL;
# Pragma endregion
 
Int MaxLength = 0;
UpdateMaxDepthLR (a, MaxLength );
 
// Output the update result
Cout <"a-> maxDepthLeft:" <a-> maxDepthLeft <endl;
Cout <"B-> maxDepthLeft:" <B-> maxDepthLeft <endl;
Cout <"c-> maxDepthLeft:" <c-> maxDepthLeft <endl;
Cout <"d-> maxDepthLeft:" <d-> maxDepthLeft <endl;
Cout <"e-> maxDepthLeft:" <e-> maxDepthLeft <endl;
Cout <"f-> maxDepthLeft:" <f-> maxDepthLeft <endl;
Cout <"g-> maxDepthLeft:" <g-> maxDepthLeft <endl;
Cout <"h-> maxDepthLeft:" Cout <"I-> maxDepthLeft:" <I-> maxDepthLeft <endl;
 
Cout <"a-> maxDepthRight:" <a-> maxDepthRight <endl;
Cout <"B-> maxDepthRight:" <B-> maxDepthRight <endl;
Cout <"c-> maxDepthRight:" <c-> maxDepthRight <endl;
Cout <"d-> maxDepthRight:" <d-> maxDepthRight <endl;
Cout <"e-> maxDepthRight:" <e-> maxDepthRight <endl;
Cout <"f-> maxDepthRight:" <f-> maxDepthRight <endl;
Cout <"g-> maxDepthRight:" <g-> maxDepthRight <endl;
Cout <"h-> maxDepthRight:" Cout <"I-> maxDepthRight:" <I-> maxDepthRight <endl;
 
Cout <"MaxLength:" <MaxLength <endl;
 
Return 0;
 
}

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.