大树大招改版的大招可以从中间过去吗

超强大风来袭,吹倒的大树不小心砸中了救护车,赶紧送救护车去汽车服务中心-母婴视频-搜狐视频
超强大风来袭,吹倒的大树不小心砸中了救护车,赶紧送救护车去汽车服务中心
视频介绍:
超强大风来袭,吹倒的大树不小心砸中了救护车,赶紧送救护车去汽车服务中心
推荐出品人
母婴热播榜7695人阅读
笔试面试(30)
书上的解法
我的思路:
在看到这题的时候,我没有马上看答案,而是自己思考了一会,得出如下思路,最后跟的思路基本一样,只是实现上略有不同。
#include &iostream&
#include &stdlib.h&
struct BinaryTreeNode
struct BinaryTreeNode *m_pL
struct BinaryTreeNode *m_pR
int maxDistance(BinaryTreeNode *root, int *max);
int DistanceCore(BinaryTreeNode *root,int *max);
//后序遍历,用于我们建立的二叉树是否正确
void Traverse( BinaryTreeNode * root);
BinaryTreeNode* Construct(int *preorder, int *inorder, int lenght);
BinaryTreeNode* ConstructCore(int *startPreorder, int *endPreorder, int *startInorder, int *endInorder);
int InsertNodeAtMostRight(BinaryTreeNode * root, BinaryTreeNode * node);
int main(int argc, char* argv[])
int preOrder[] = {5, 4, 8, 9, 6, 3, 18, 19, 2};
int inOrder[] = {9, 8, 6, 3, 4, 5, 19, 18, 2};
BinaryTreeNode *parent = Construct(preOrder, inOrder, sizeof(inOrder) / sizeof(inOrder[0]));
cout && &A树的后序遍历的结果:& &&
Traverse(parent);
BinaryTreeNode *node1 = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
BinaryTreeNode *node2 = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
node1-&m_nValue = 0;
node1-&m_pLeft = NULL;
node1-&m_pRight = NULL;
node2-&m_nValue = 0;
node2-&m_pLeft = NULL;
node2-&m_pRight = NULL;
maxDistance(parent, &max);
cout &&&max distance of tree's nodes : & && max &&
InsertNodeAtMostRight(parent, node1);
maxDistance(parent, &max);
cout &&&max distance of tree's nodes after insert node1: & && max &&
InsertNodeAtMostRight(parent, node2);
maxDistance(parent, &max);
cout &&&max distance of tree's nodes after insert node2: & && max &&
//测试极端情况,即只有一个节点
maxDistance(node2, &max);
cout &&&just one node & && max &&
//测试极端情况,即只有二个节点
maxDistance(node1, &max);
cout &&&just two node & && max &&
BinaryTreeNode* Construct(int *preorder, int *inorder, int lenght)
if (preorder == NULL || inorder == NULL || lenght &= 0)
return NULL;
return ConstructCore(preorder, preorder + lenght - 1, inorder, inorder + lenght - 1);
BinaryTreeNode* ConstructCore(int *startPreorder, int *endPreorder, int *startInorder, int *endInorder)
int rootValue = startPreorder[0];
BinaryTreeNode *root = new BinaryTreeNode();
root-&m_nValue = rootV
root-&m_pLeft = root-&m_pRight = NULL;
if (startPreorder == endPreorder)
{//先序遍历已经结束了,那这个时候一定是插入最后一个节点,则应该满足下面的if语句,否则输入的数据有误
if (startInorder == endInorder && *startPreorder == *startInorder)
cout && &Invalid input& &&
int *rootInorder = startI
while (rootInorder &= endInorder && *rootInorder != rootValue)
if (rootInorder &= endInorder && *rootInorder != rootValue)
cout && &Invalid input& &&
int leftLength = rootInorder - startI
int *leftPreorderEnd = startPreorder + leftL
if (leftLength & 0)
root-&m_pLeft = ConstructCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder - 1);
if (leftLength & endPreorder - startPreorder)
root-&m_pRight = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);
void Traverse( BinaryTreeNode * root)
if (root == NULL)
Traverse(root-&m_pLeft);
Traverse(root-&m_pRight);
cout && root-&m_nValue && &
int maxDistance(BinaryTreeNode *root, int *max)
//这个函数的主要功能是判断root不为空,且给max赋初值
if (root == NULL || max == NULL)
return -1;
return DistanceCore(root, max);
int DistanceCore(BinaryTreeNode *root, int *max)
//如果节点是叶子节点,则返回0——深度
if (root-&m_pLeft == NULL && root-&m_pRight == NULL)
//保存左右子树的最大深度
int lDistance = 0;
int rDistance = 0;
//左子树不为空,返回当前节点到左子树的最大深度
if (root-&m_pLeft != NULL)
lDistance = 1 + DistanceCore(root-&m_pLeft, max);
if (root-&m_pRight != NULL)
rDistance = 1 + DistanceCore(root-&m_pRight, max);
//遍历到当前节点时,能获得的最大距离
if (lDistance + rDistance & *max)
//保存当前获得的最大距离
*max = lDistance + rD
//返回左右子树中,深度较大的一个
return lDistance & rDistance ? lDistance : rD
//为了测试程序写的辅助函数,在树的最最右边插入一个新的节点
int InsertNodeAtMostRight(BinaryTreeNode * root, BinaryTreeNode * node)
if (root == NULL || node == NULL)
return -1;
while (root-&m_pRight != NULL)
root = root-&m_pR
root-&m_pRight =
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:697308次
积分:7668
积分:7668
排名:第2643名
原创:135篇
转载:35篇
评论:119条
本博客乃学习笔记,没有纯粹无意义的转载。作者除了对自己负责,不对任何读者负责。欢迎指出文章错误,如果原意交朋友,可以通过Gmail联系我(),博客基本不再更新,欢迎访问我的独立博客
(5)(2)(1)(1)(14)(22)(11)(48)(30)(17)(22)(4)

我要回帖

更多关于 lol大树的大招 的文章

 

随机推荐