很容易的一道题,构建树模型就好了
#include <iostream>
#include <vector>
using namespace std;
struct treeNode {
int val;
treeNode* left;
treeNode* right;
treeNode(int val_) {
val = val_;
left = NULL;
right = NULL;
}
treeNode() {
left = NULL;
right = NULL;
}
};
struct info {
treeNode* node;
int left, right;
info(treeNode* node_, int left_, int right_) {
node = node_;
left = left_;
right = right_;
}
};
treeNode* findParent(treeNode* root, int val) {
if (!root || (!root->right && !root->left)) {
return NULL;
}
if (((root->left) && (root->left->val == val)) || ((root->right) && (root->right->val == val))) {
return root;
}
treeNode* findLeft = findParent(root->left, val);
treeNode* findRight = findParent(root->right, val);
if (!findLeft) {
return findRight;
}
return findLeft;
}
treeNode* findVal(treeN