文章目录
- 一、题目
- 二、解法
- 三、完整代码
所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。
一、题目
二、解法
思路分析: 根据定义,最近祖先节点需要遍历节点的左右子树,然后才能知道是否为最近祖先节点。那么这种需求是先遍历左右节点,然后遍历中间节点,属于左右中的后序遍历模式。因此在程序当中,我们选择递归中序遍历。输入参数为根节点p q节点。终止条件是当前节点和p q当中任意一个节点相等时就返回,遍历到空节点也返回。因为是后序遍历,根据遍历完左右子树后的返回值确定返回参数,如果返回值都不为空,则当前节点就是最近祖先节点。如果left为空,right不为空,则最近祖先节点在右子树,反之亦然。均为空则返回NULL。
程序如下:
class Solution2 {
public:// 后序遍历: 左右中// 1、输入参数TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {// 2、终止条件if (root == q || root == p || root == NULL) return root; // 如果节点相等或者是空节点返回// 3、单层递归逻辑TreeNode* left = lowestCommonAncestor(root->left, p, q); // 左TreeNode* right = lowestCommonAncestor(root->right, p, q); // 右// 1、返回值if (left != NULL && right != NULL) return root;if (left == NULL && right != NULL) return right;else if (left != NULL && right == NULL) return left;else { // (left == NULL && right == NULL)return NULL;}}
};
代码优化:
class Solution {
public:// 后序遍历: 左右中// 1、输入参数TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {// 2、终止条件if (root == p || root == q || root == NULL) return root; // 如果节点相等或者是空节点返回// 3、单层递归逻辑TreeNode* left = lowestCommonAncestor(root->left, p, q); // 左TreeNode* right = lowestCommonAncestor(root->right, p, q); // 右if (left != NULL && right != NULL) return root;if (left == NULL) return right;// 1、返回值return left;}
};
三、完整代码
# include <iostream>
# include <vector>
# include <string>
# include <queue>
using namespace std;// 树节点定义
struct TreeNode {int val;TreeNode* left;TreeNode* right;TreeNode() : val(0), left(nullptr), right(nullptr) {}TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};class Solution {
public:// 后序遍历: 左右中// 1、输入参数TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {// 2、终止条件if (root == p || root == q || root == NULL) return root; // 如果节点相等或者是空节点返回// 3、单层递归逻辑TreeNode* left = lowestCommonAncestor(root->left, p, q); // 左TreeNode* right = lowestCommonAncestor(root->right, p, q); // 右if (left != NULL && right != NULL) return root;if (left == NULL) return right;// 1、返回值return left;}
};class Solution2 {
public:// 后序遍历: 左右中// 1、输入参数TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {// 2、终止条件if (root == q || root == p || root == NULL) return root; // 如果节点相等或者是空节点返回// 3、单层递归逻辑TreeNode* left = lowestCommonAncestor(root->left, p, q); // 左TreeNode* right = lowestCommonAncestor(root->right, p, q); // 右// 1、返回值if (left != NULL && right != NULL) return root;if (left == NULL && right != NULL) return right;else if (left != NULL && right == NULL) return left;else { // (left == NULL && right == NULL)return NULL;}}
};// 前序遍历迭代法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>& t, TreeNode*& node) {if (!t.size() || t[0] == "NULL") return; // 退出条件else {node = new TreeNode(stoi(t[0].c_str())); // 中if (t.size()) {t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->left); // 左}if (t.size()) {t.assign(t.begin() + 1, t.end());Tree_Generator(t, node->right); // 右}}
}template<typename T>
void my_print(T& v, const string msg)
{cout << msg << endl;for (class T::iterator it = v.begin(); it != v.end(); it++) {cout << *it << ' ';}cout << endl;
}template<class T1, class T2>
void my_print2(T1& v, const string str) {cout << str << endl;for (class T1::iterator vit = v.begin(); vit < v.end(); ++vit) {for (class T2::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {cout << *it << ' ';}cout << endl;}
}// 层序遍历
vector<vector<int>> levelOrder(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<vector<int>> result;while (!que.empty()) {int size = que.size(); // size必须固定, que.size()是不断变化的vector<int> vec;for (int i = 0; i < size; ++i) {TreeNode* node = que.front();que.pop();vec.push_back(node->val);if (node->left) que.push(node->left);if (node->right) que.push(node->right);}result.push_back(vec);}return result;
}// 前序遍历,找二叉树中指定的键值
TreeNode* traversal_preOrder(TreeNode* cur, int val) {if (cur == NULL) return NULL;if (cur->val == val) return cur; // 中if (traversal_preOrder(cur->left, val) != NULL) return traversal_preOrder(cur->left, val); // 左 if (traversal_preOrder(cur->right, val) != NULL) return traversal_preOrder(cur->right, val); // 右 return NULL;
}int main()
{// 构建二叉树vector<string> t = { "3", "5", "6", "NULL", "NULL", "2", "7", "NULL", "NULL", "4", "NULL", "NULL", "1", "0", "NULL", "NULL", "8", "NULL", "NULL"}; // 前序遍历my_print(t, "目标树");TreeNode* root = new TreeNode();Tree_Generator(t, root);vector<vector<int>> tree = levelOrder(root);my_print2<vector<vector<int>>, vector<int>>(tree, "目标树:");// 构建p, q节点int p = 5, q = 1;TreeNode* P_node = traversal_preOrder(root, p);TreeNode* Q_node = traversal_preOrder(root, q);// 找最近公共祖先节点Solution s;TreeNode* result = s.lowestCommonAncestor(root, P_node, Q_node);cout << "节点 " << P_node->val << " 和节点 " << Q_node->val << " 的最近公共祖先节点为 " << result->val << endl;system("pause");return 0;
}
end