第11 题
求二叉树中节点的最大距离...
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,
我们姑且定义"距离"为两节点之间边的个数。
写一个程序,
求一棵二叉树中相距最远的两个节点之间的距离。
/*
分析:
1、具有最大距离的两个节点必定是叶子节点,如果不是叶子节点,那么我们可以用该节点的叶子节点代替,生成更大的距离。
2、其中一个节点必定是具有最大深度的叶子节点。
3、如果存在两个或者多个具有最大深度的叶子节点,那么由任意一个其中的节点生成的最大距离就是最大距离。
《编程之美》上有该题目:
1、最大距离的两个节点是两个叶子节点 或者 一个叶子节点,一个根节点
2、每个节点左右子树的最大深度之和 就是该节点为根节点的最大距离。递归比较.
Learn: 树的一个很重要的特性就是递归性。
*/
#include <vector>
#include <cassert>
#include <iostream>
using namespace std;
struct TreeNode
{
int data;
struct TreeNode *parent;
struct TreeNode *leftChild;
struct TreeNode *rightChild;
int height;
};
class MaxDistanceTree
{
public:
MaxDistanceTree(TreeNode *rootNode)
{
root = rootNode;
leafNodes.clear();
maxHeightNode = NULL;
initSelfElements();
}
void initSelfElements() // set set heights, leafNodes, maxHeightNode;
{
root->height = 0;
setElement(root);
}
void setElement(TreeNode *rootNode)
{
assert(rootNode!= NULL);
if (rootNode->parent == NULL)
{
rootNode->height = 0;
}
else
{
rootNode->height = rootNode->parent->height + 1;
if ((rootNode->leftChild == NULL) && (rootNode->rightChild == NULL))
{
leafNodes.push_back(rootNode);
if ((maxHeightNode == NULL) || (maxHeightNode->height < rootNode->height))
maxHeightNode = rootNode;
}
}
if (rootNode->leftChild != NULL)
setElement(rootNode->leftChild);
if (rootNode->rightChild != NULL)
setElement(rootNode->rightChild);
}
int getMaxDistance()
{
int distance = 0;
int maxDistance = 0;
for (int i = 0; i < leafNodes.size(); i++)
{
distance = getDistance(maxHeightNode, leafNodes[i]);
if (distance > maxDistance)
maxDistance = distance;
}
return maxDistance;
}
private:
int getDistance(TreeNode *node1, TreeNode *node2)
{
if (node1 == node2)
return 0;
int distance = 0;
std::vector<TreeNode *>stack1;
std::vector<TreeNode *>stack2;
TreeNode *temp = node1;
while(temp)
{
stack1.push_back(temp);
temp = temp->parent;
}
temp = node2;
while(temp)
{
stack2.push_back(temp);
temp = temp->parent;
}
while(stack1[stack1.size() - 1] == stack2[stack2.size() - 1]) //the same parent
{
stack1.pop_back();
stack2.pop_back();
}
if ((stack1.size() != 0) && (stack2.size() != 0))
{
int distance1 = node1->height - stack1[stack1.size() - 1]->height + 1;
int distance2 = node2->height - stack2[stack2.size() - 1]->height + 1;
distance = distance1 + distance2;
}
return distance;
}
TreeNode *root;
std::vector<TreeNode *>leafNodes;
TreeNode *maxHeightNode;
};
/*
int main(void)
{
TreeNode parent;
TreeNode leftChild;
TreeNode rightChild;
TreeNode rleftChild;
TreeNode rrightChild;
parent.parent = NULL;
parent.leftChild = &leftChild;
parent.rightChild = &rightChild;
leftChild.parent = &parent;
leftChild.leftChild = NULL;
leftChild.rightChild = NULL;
rightChild.parent = &parent;
rightChild.leftChild = &rleftChild;
rightChild.rightChild = &rrightChild;
rleftChild.parent = &rightChild;
rleftChild.leftChild = NULL;
rleftChild.rightChild = NULL;
rrightChild.parent = &rightChild;
rrightChild.leftChild = NULL;
rrightChild.rightChild = NULL;
MaxDistanceTree tree(&parent);
cout << tree.getMaxDistance() << endl;
system("Pause");
return 0;
}*/