c++实现二叉树中节点的最大距离 - c++语言程序开发技术文章,求二叉树中节点间的最大距离(c++代码完整实现)...

//求一个二叉树中节点间的最大距离,

//两个节点的距离定义是这两个节点间的边的个数

//比如某个孩子节点和父节点间的距离是1,和相邻兄弟节点间的距离是2,优化时间复杂度

#include

#include

#define OVERFLOW -2

using namespace std;

// 数据结构定义

struct NODE

{

NODE* pLeft; // 左子树

NODE* pRight; // 右子树

int nMaxLeft; // 左子树中的最长距离

int nMaxRight; // 右子树中的最长距离

char chValue; // 该节点的值

};

int nMaxLen = 0;

// 寻找树中最长的两段距离

void FindMaxLen(NODE* pRoot)

{

// 遍历到叶子节点,返回

if(pRoot == NULL)

{

return;

}

// 如果左子树为空,那么该节点的左边最长距离为0

if(pRoot -> pLeft == NULL)

{

pRoot -> nMaxLeft = 0;

}

// 如果右子树为空,那么该节点的右边最长距离为0

if(pRoot -> pRight == NULL)

{

pRoot -> nMaxRight = 0;

}

// 如果左子树不为空,递归寻找左子树最长距离

if(pRoot -> pLeft != NULL)

{

FindMaxLen(pRoot -> pLeft);

}

// 如果右子树不为空,递归寻找右子树最长距离

if(pRoot -> pRight != NULL)

{

FindMaxLen(pRoot -> pRight);

}

// 计算左子树最长节点距离

if(pRoot -> pLeft != NULL)

{

int nTempMax = 0;

if(pRoot -> pLeft -> nMaxLeft > pRoot -> pLeft -> nMaxRight)

{

nTempMax = pRoot -> pLeft -> nMaxLeft;

}

else

{

nTempMax = pRoot -> pLeft -> nMaxRight;

}

pRoot -> nMaxLeft = nTempMax + 1;

}

// 计算右子树最长节点距离

if(pRoot -> pRight != NULL)

{

int nTempMax = 0;

if(pRoot -> pRight -> nMaxLeft > pRoot -> pRight -> nMaxRight)

{

nTempMax = pRoot -> pRight -> nMaxLeft;

}

else

{

nTempMax = pRoot -> pRight -> nMaxRight;

}

pRoot -> nMaxRight = nTempMax + 1;

}

// 更新最长距离

if(pRoot -> nMaxLeft + pRoot -> nMaxRight > nMaxLen)

{

nMaxLen = pRoot -> nMaxLeft + pRoot -> nMaxRight;

}

}

bool CreateBiTree(NODE ** pTree){

char ch;

cin>>ch;

if(ch =='#') *pTree=NULL;

else{

if(!((*pTree)=(NODE*)malloc(sizeof(NODE))))

exit(OVERFLOW);

(*pTree)->nMaxLeft=0;

(*pTree)->nMaxRight=0;

(*pTree)->chValue=ch;

CreateBiTree(&(*pTree)->pLeft);

CreateBiTree(&(*pTree)->pRight);

}

return true;

}

int main(int argc, char *argv[])

{

NODE *pRoot=NULL;

bool flag=CreateBiTree(&pRoot);

FindMaxLen(pRoot);

cout<

system("PAUSE");

return EXIT_SUCCESS;

}

编译后运行该程序,按照先序遍历的顺序建立二叉树,比如输入:abc##dg####,会建立一颗如下的二叉树:

3995794.htm

1254408268557fb372968b98f8d20123.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现二叉树从指定节点到根节点的路径,可以通过递归实现。具体实现步骤如下: 1. 定义二叉树节点结构体,包含左右子节点指针和节点值。 ```cpp struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; ``` 2. 定义函数 `pathToRoot`,接收一个指向二叉树节点的指针,返回从该节点到根节点的路径。 ```cpp vector<int> pathToRoot(TreeNode* node); ``` 3. 在函数内部实现递归操作,如果当前节点不为空,则将当前节点的值加入结果数组,然后递归遍历该节点的父节点,直到遍历到根节点停止。 ```cpp vector<int> pathToRoot(TreeNode* node) { vector<int> res; if (node) { res.push_back(node->val); vector<int> parent = pathToRoot(node->parent); res.insert(res.end(), parent.begin(), parent.end()); } return res; } ``` 完整代码如下: ```cpp #include <iostream> #include <vector> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; vector<int> pathToRoot(TreeNode* node) { vector<int> res; if (node) { res.push_back(node->val); vector<int> parent = pathToRoot(node->parent); res.insert(res.end(), parent.begin(), parent.end()); } return res; } int main() { TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); vector<int> path = pathToRoot(root->left->right); for (int i = 0; i < path.size(); i++) { cout << path[i] << " "; } cout << endl; return 0; } ``` 输出结果为: `5 2 1`,表示从节点 5 到根节点 1 的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值