Lowest Common Ancestor of a Binary Tree Solutions in C++
Number 236
Difficulty Medium
Acceptance 45.8%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/// Author : Hao Chen// Date : 2015-07-17/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:bool findPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path) {if (root==NULL) return false;if (root == p) {path.push_back(p);return true;}path.push_back(root);if (findPath(root->left, p, path)) return true;if (findPath(root->right, p, path)) return true;path.pop_back();return false;}//Ordinary way, find the path and comapre the path.TreeNode* lowestCommonAncestor01(TreeNode* root, TreeNode* p, TreeNode* q) {vector<TreeNode*> path1, path2;if (!findPath(root, p, path1)) return NULL;if (!findPath(root, q, path2)) return NULL;int len = path1.size() < path2.size() ? path1.size() : path2.size();TreeNode* result = root;for(int i=0; i<len; i++) {if (path1[i] != path2[i]) {return result;}result = path1[i];}return result;}//Actually, we can do the recursive search in one timeTreeNode* lowestCommonAncestor02(TreeNode* root, TreeNode* p, TreeNode* q) {//return if found or not found, return NULL if not foundif (root==NULL || root == p || root == q) return root;//find the LCA in left treeTreeNode* left = lowestCommonAncestor02(root->left, p, q);//find the LCA in right treeTreeNode* right = lowestCommonAncestor02(root->right, p, q);//left==NULL means both `p` and `q` are not found in left tree.if (left==NULL) return right;//right==NULL means both `p` and `q` are not found in right tree.if (right==NULL) return left;// left!=NULL && right !=NULL, which means `p` & `q` are seperated in left and right tree.return root;}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {srand(time(0));if (random()%2) {return lowestCommonAncestor02(root, p, q);}return lowestCommonAncestor01(root, p, q);}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description//// Author : liuyubobobo/// Time : 2020-04-22#include <iostream>#include <vector>using namespace std;/// Find two paths/// Time Complexity: O(3n)/// Space Complexity: O(2n)///Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {vector<TreeNode*> path1;dfs(root, p, path1);vector<TreeNode*> path2;dfs(root, q, path2);TreeNode* res;for(int i = 0; i < path1.size() && i < path2.size(); i ++)if(path1[i] == path2[i]) res = path1[i];else break;return res;}private:bool dfs(TreeNode* node, TreeNode* target, vector<TreeNode*>& path){if(!node) return false;path.push_back(node);if(node == target) return true;if(dfs(node->left, target, path)) return true;if(dfs(node->right, target, path)) return true;path.pop_back();return false;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description//// Author : liuyubobobo/// Time : 2017-01-30#include <iostream>#include <cassert>using namespace std;/// Recursion implementation/// Time Complexity: O(n)/// Space Complexity: O(n)///Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:// 在root中寻找p和q// 如果p和q都在root所在的二叉树中, 则返回LCA// 如果p和q只有一个在root所在的二叉树中, 则返回p或者q// 如果p和q均不在root所在的二叉树中, 则返回NULLTreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root == NULL)return root;if(root == p || root == q)return root;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 left;if(right != NULL)return right;return NULL;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description//// Author : liuyubobobo/// Time : 2018-11-17#include <iostream>#include <cassert>using namespace std;/// Another Recursion implementation/// Time Complexity: O(n)/// Space Complexity: O(n)///Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {private:TreeNode* ret = 0;public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {dfs(root, p, q);assert(ret);return ret;}private:// 在root中寻找p和q, 如果包含则返回 true// 否则返回false//// root是p或者q;root的左子树包含p或q;root的右子树包含p或q;三个条件有两个满足// 则 ret = rootbool dfs(TreeNode* root, TreeNode* p, TreeNode* q) {if(!root)return false;bool mid = false;if(root == p || root == q)mid = true;bool left = dfs(root->left, p, q);bool right = dfs(root->right, p, q);if(mid + left + right >= 2)ret = root;return mid + left + right;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description//// Author : liuyubobobo/// Time : 2020-04-22#include <iostream>#include <vector>#include <map>#include <cmath>using namespace std;/// Binary Lifting/// Time Complexity: init: O(nlogn)/// query: O(logn)/// Space Complexity: O(nlogn)///Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {private:map<TreeNode*, int> tin, tout;map<TreeNode*, TreeNode*> parents;vector<TreeNode*> nodes;map<TreeNode*, vector<TreeNode*>> up;public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {int time = 0;int depth = dfs(root, root, time);if(isAncestor(p, q)) return p;if(isAncestor(q, p)) return q;// upfor(TreeNode* node: nodes)up[node].push_back(parents[node]);int d = ceil(log2(depth));for(int i = 1; i <= d; i ++)for(TreeNode* node: nodes)up[node].push_back(up[up[node][i - 1]][i - 1]);// lcafor(int i = d; i >= 0; i --)if(!isAncestor(up[p][i], q))p = up[p][i];return up[p][0];}private:// a is x's ancestor?bool isAncestor(TreeNode* a, TreeNode* x){return tin[a] <= tin[x] && tout[a] >= tout[x];}int dfs(TreeNode* node, TreeNode* p, int& time){if(!node) return 0;nodes.push_back(node);parents[node] = p;int depth = 0;tin[node] = time ++;depth = max(depth, 1 + dfs(node->left, node, time));depth = max(depth, 1 + dfs(node->right, node, time));tout[node] = time ++;return depth;}};int main() {// [-1,0,3,-2,4,null,null,8]// 8// 4// Expected: 0return 0;}