求二叉树指定结点到根结点的路径c++ 非常详细。

 看了很多 没有看见完整的代码 我喜欢喂饭喂到嘴边。

部分代码参考16 二叉树:以x为根的子树的深度_DHU杨骅麟(紫外线过敏)的博客-CSDN博客

面试经典(16)--二叉树根节点到指定节点的路径_nginux的博客-CSDN博客_二叉树根节点到目标节点路径

 运行示例:

 

#include<iostream>
#include<stack>
#include<vector>
using namespace std;
struct Node
{
    string data;
	Node* left;
	Node* right;
};
//查找二叉树指定节点

Node* target = NULL;
void creat(Node*& T, string kk)
{
    string ch;
    cin >> ch;
    if (ch == kk)
    {
        T = NULL;
    }//但凡输入了#号 该节点下一位停止
    else
    {
        T = new Node;
        T->data = ch;
        creat(T->left, kk);
        creat(T->right, kk);
    }
}


void find(Node* root, string x)//用来寻找结点的地址 
{
	Node* p = root;
	if (p == NULL)
	{
		return;
	}
	else if (p->data == x)
	{

		target = p;//核心 找到p的地址 抓包给全局变量 target现在的值就是所求的地址
	}

	else if (p != NULL && p->data != x)
	{
		find(p->left, x);
		find(p->right, x);
	}
}




bool display(Node* pRoot, Node* pNode, vector<string>& v)
{
	if (pRoot == NULL)
	{
		return false;
	}
	v.push_back(pRoot->data);
	bool found = false;
	if (pRoot == pNode)//还是比较指针稳妥,节点值有可能重复
	{
		for (int i = 0; i < v.size(); i++)
			cout << v[i] << " ";
		cout << endl;
		return true;
	}
	if (!found && pRoot->left)
	{
		found = display(pRoot->left, pNode, v);
	}

	//一旦左子树中找到节点,就不需要再遍历右子树
	if (!found && pRoot->right)
	{
		found = display(pRoot->right, pNode, v);
	}
	if (!found)
		v.pop_back();
	return found;
}



int main()
{
	Node* root;
	string kk;
	cout << "请输入表示终止的符号:" << endl;
	cin >> kk;
	cout << "现在请输入你的二叉树 请用先序遍历输入" << endl;
	creat(root, kk);
	vector<string>m;
	cout << "输入你要找的节点" << endl;
	string x;
	cin >> x;	
	find(root, x);
	cout << "路径如下:" << endl;
	display(root, target, m);
	return 0;

}

  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
二叉树的每个结点都有两个指针,指向其左、右子树,可以使用递归的方式来指定结点节点的路径。 具体步骤如下: 1. 如果当前结点为空,则返回false。 2. 如果当前结点为目标结点,则返回true。 3. 递归遍历左子树,如果左子树中存在目标结点,则将当前结点加入路径中,并返回true。 4. 递归遍历右子树,如果右子树中存在目标结点,则将当前结点加入路径中,并返回true。 5. 如果左、右子树都不存在目标结点,则返回false。 6. 如果返回true,则将当前结点加入路径中。 7. 返回路径。 下面是C语言实现代码: ```c #include <stdio.h> #include <stdbool.h> // 二叉树结点结构体 struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; // 递归路径 bool findPath(struct TreeNode* root, int target, int* path, int* pathLen) { // 如果当前结点为空,则返回false if (root == NULL) { return false; } // 如果当前结点为目标结点,则返回true if (root->val == target) { path[*pathLen] = root->val; (*pathLen)++; return true; } // 递归遍历左子树 if (findPath(root->left, target, path, pathLen)) { path[*pathLen] = root->val; (*pathLen)++; return true; } // 递归遍历右子树 if (findPath(root->right, target, path, pathLen)) { path[*pathLen] = root->val; (*pathLen)++; return true; } // 如果左、右子树都不存在目标结点,则返回false return false; } // 指定结点节点的路径 int* getPath(struct TreeNode* root, int target, int* returnSize) { // 定义路径数组 int* path = (int*)malloc(1000 * sizeof(int)); // 定义路径长度 int pathLen = 0; // 递归路径 findPath(root, target, path, &pathLen); // 将路径数组反转,得到从节点到指定结点路径 int i, j; for (i = 0, j = pathLen - 1; i < j; i++, j--) { int temp = path[i]; path[i] = path[j]; path[j] = temp; } // 设置返回值 *returnSize = pathLen; // 返回路径数组 return path; } ``` 使用方法如下: ```c int main() { // 构建二叉树 struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = 1; root->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->left->val = 2; root->left->left = NULL; root->left->right = NULL; root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->val = 3; root->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->left->val = 4; root->right->left->left = NULL; root->right->left->right = NULL; root->right->right = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->right->right->val = 5; root->right->right->left = NULL; root->right->right->right = NULL; // 路径 int* path; int pathLen; path = getPath(root, 4, &pathLen); // 输出路径 int i; for (i = 0; i < pathLen; i++) { printf("%d ", path[i]); } printf("\n"); return 0; } ``` 输出结果为:3 4

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨骅麟(Patrick Young)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值