|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 糖逗 于 2020-5-8 18:06 编辑
题目描述:给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉搜索树中。
#include <iostream>
#include <vector>
#include <string>
#include<malloc.h>
#include <math.h>
using namespace std;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL), right(NULL){
}
};
TreeNode* CreateTree(vector<int>& input){
TreeNode* tree = (TreeNode*)malloc(sizeof(TreeNode)*input.size());
for(int i = 0; i < input.size() ;i++){
tree[i].val = input[i];
tree[i].left = NULL;
tree[i].right = NULL;
}
for(int i = 0; i <= input.size()/2-1; i++){
if(2*i+1 <= input.size()){
tree[i].left = &tree[2*i+1];
}
if(2*i+2<=input.size()){
tree[i].right = &tree[2*i+2];
}
}
return tree;
}
void middle(TreeNode* root, vector<vector<int> >& res, int left, int right, int depth){
if(root == NULL) return;
int insert = left + (right - left) / 2;
res[depth][insert] = root->val;
middle(root->left, res, left, insert - 1, depth + 1);
middle(root->right, res, insert + 1, right, depth + 1);
}
int treeDepth(TreeNode* root){
if(root == NULL) return 0;
return max(treeDepth(root->left) + 1, treeDepth(root->right) + 1);
}
void printTree(TreeNode* root) {
int depth = treeDepth(root);
int width = pow(2, depth) - 1;
vector<vector<int> > res(depth, vector<int>(width, 0));
middle(root, res, 0, width - 1, 0);
for(int i = 0; i < res.size(); i++){
for(int j = 0; j < res[i].size();j++){
if(res[i][j] == 0){
cout << " ";
}
else{
cout << res[i][j];
}
}
cout << endl;
}
cout << "------------------" << endl;
}
TreeNode* findNode(TreeNode*root, int number1){
if(root -> val == number1) return root;
if(root -> val > number1 && root->left != NULL) return findNode(root -> left, number1);
if(root -> val < number1 && root->right != NULL) return findNode(root -> right, number1);
return NULL;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if((root->val-p->val)*(root->val-q->val) <= 0) return root;
if(root -> val < p -> val) return lowestCommonAncestor(root -> right, p, q);
if(root -> val > p -> val) return lowestCommonAncestor(root -> left, p, q);
return NULL;
}
int main(void){
vector<int>input;
int number;
cout << "please send numbers for this tree:" << endl;
while(cin >> number){
input.push_back(number);
}
cin.clear();
TreeNode* root = CreateTree(input);
printTree(root);
cout << "please send the first number that you want to find in this tree" << endl;
int number1;
cin >> number1;
cin.clear();
TreeNode* find_number1 = findNode(root, number1);
printTree(find_number1);
cout << "please send the second number that you want to find in this tree" << endl;
int number2;
cin >> number2;
TreeNode* find_number2 = findNode(root, number2);
printTree(find_number2);
TreeNode* res = lowestCommonAncestor(root, find_number1, find_number2);
printTree(res);
return 0;
}
注意事项:
1.这里的代码包括输入vector转化为二叉树、将二叉树打印、寻找二叉树中等于目标值的根节点树、返回两个树之间公共根节点的完整树。
2.这里的输入必须保证根节点介于左右两个节点之间:左节点值<根节点值<右节点值 例如[4,2,8,1,3,7,9]可以,[1,2,3,4,5,6,7]不可以。 |
|