#include <iostream>
using namespace std;
/*
*二叉树最近父节点LCS
*/
typedef struct btree {
int data;
struct btree* lchild;
struct btree* rchild;
btree(int num) {
data = num;
lchild = rchild = NULL;
}
}node;
void initBtree(node* &root) {
int num;
cin >> num;
if (num) {
root = new node(num);
initBtree(root->lchild);
initBtree(root->rchild);
}
else {
root = NULL;
}
}
int LCS(node* node, int p, int q) {
if (node == NULL) {
return 0;
}
if (node->data == p || node->data == q) {
return node->data;
}
if (LCS(node->lchild, p, q) && LCS(node->rchild, p, q)) {
return node->data;
}
else if (!LCS(node->lchild, p, q) && LCS(node->rchild, p, q)) {
return LCS(node->rchild, p, q);
}
else {
return LCS(node->lchild, p, q);
}
}
二叉树最近公共父节点LCS问题---C++实现
最新推荐文章于 2022-11-10 18:28:48 发布