1. 程式人生 > >求二叉樹中任意兩個結點間的路徑(C++)

求二叉樹中任意兩個結點間的路徑(C++)

方法一

#include <iostream>
#include <vector>
using namespace std;
struct Node
{
	int val;
	Node* left;
	Node* right;
	Node(int v)
	{
		val = v;
		left = nullptr;
		right = nullptr;
	}
};
void GetNodePath(Node* root, Node* node, vector<Node*>& v, bool& flag)
//用後根遍歷的方式尋找node,找到後儲存從該節點到根節點的路徑
{
	if (root == NULL || flag == true)//根為空或者node已經找到了
		return;
	GetNodePath(root->left, node, v, flag);
	GetNodePath(root->right, node, v, flag);
	if (root->val == node->val || flag == true)//找到node或者root為node到根節點路徑上的節點
	{
		v.push_back(root);//儲存從node到根節點的路徑
		flag = true;
	}
}
Node* CreatTree()//初始化樹
{
	Node* root = new Node(1);
	Node* node1 = new Node(2);
	Node* node2 = new Node(3);
	Node* node3 = new Node(4);
	Node* node4 = new Node(5);
	Node* node5 = new Node(6);
	Node* node6 = new Node(7);
	root->left = node1;
	root->right = node2;
	node1->left = node3;
	node1->right = node4;
	node2->left = node5;
	node2->right = node6;
	return root;
}
int main()
{
	Node* root = CreatTree();
	Node* node1 = new Node(5);
	Node* node2 = new Node(6);
	bool flag = false;
	vector<Node*> path1;//用於記錄路徑
	vector<Node*> path2;
	GetNodePath(root, node1, path1, flag);
	flag = false;
	GetNodePath(root, node2, path2, flag);
	//下面程式碼用於求兩個連結串列的第一個公共節點
	int i = 0, j = 0;
	if (path1.size() > path2.size())
		i = path1.size() - path2.size();
	else
		j = path2.size() - path1.size();
	int res = -1;
	while (i < path1.size() && j < path2.size())
	{
		if (path1[i]->val == path2[j]->val)
		{
			res = path1[i]->val;
			break;
		}
		i++;
		j++;
	}
	cout << res << endl;
	cout << i + j;
	cout << endl;
	system("pause");
	return 0;
}

方法二

#include <iostream>
#include <vector>
using namespace std;
struct Node
{
	int val;
	Node* left;
	Node* right;
	Node(int v)
	{
		val = v;
		left = nullptr;
		right = nullptr;
	}
};
Node* CreatTree()
{
	Node* root = new Node(1);
	Node* node1 = new Node(2);
	Node* node2 = new Node(3);
	Node* node3 = new Node(4);
	Node* node4 = new Node(5);
	Node* node5 = new Node(6);
	Node* node6 = new Node(7);
	root->left = node1;
	root->right = node2;
	node1->left = node3;
	node1->right = node4;
	node2->left = node5;
	node2->right = node6;
	return root;
}
Node* FindFather(Node* root, Node* node1, Node* node2, int& flag)
{
	if (flag == 2)
		return nullptr;
	if (root == nullptr)
		return nullptr;
	Node* left = FindFather(root->left, node1, node2, flag);
	Node* right = FindFather(root->right, node1, node2, flag);
	if (left && right)
		return root;
	if (root->val == node1->val)
	{
		flag++;
		return root;
	}
	if (root->val == node2->val)
	{
		flag++;
		return root;
	}
	if (left == nullptr && right)
		return right;
	if (right == nullptr && left)
		return left;
	return nullptr;
}
int FindPath(Node* root, Node* node, int &flag)
{
	if (flag == 1)
		return -1;
	if (root == nullptr || node == nullptr)
		return -1;
	if (root->val == node->val)
	{
		flag = 1;
		return 0;
	}
	int left = FindPath(root->left, node, flag);
	int right = FindPath(root->right, node, flag);
	if (left >= 0)
		return left + 1;
	else if (right >= 0)
		return right + 1;
	else
		return -1;
}
int main()
{
	Node* root = CreatTree();
	Node* node1 = new Node(6);
	Node* node2 = new Node(5);
	int flag = 0;
	Node* father = FindFather(root, node1, node2, flag);
	int flag1 = 0, flag2 = 0;
	cout << FindPath(father, node1, flag1) + FindPath(father, node2, flag2) << endl;
	system("pause");
	return 0;
}