Skip to content

Commit

Permalink
Create 1644.Lowest-Common-Ancestor-of-a-Binary-Tree-II.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Nov 8, 2020
1 parent af2e3c4 commit 8207243
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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);
reverse(path1.begin(), path1.end());

vector<TreeNode*>path2 = dfs(root, q);
reverse(path2.begin(), path2.end());

if (path1.size()==0 ||path2.size()==0)
return NULL;

int i=0;
while (i<path1.size() && i<path2.size() && path1[i]==path2[i])
i++;
return path1[i-1];

}

vector<TreeNode*>dfs(TreeNode* root, TreeNode* p)
{
if (root==NULL) return {};
if (root==p) return {root};
vector<TreeNode*>path = dfs(root->left, p);
if (!path.empty())
{
path.push_back(root);
return path;
}
path.clear();
path = dfs(root->right, p);
if (!path.empty())
{
path.push_back(root);
return path;
}
return {};
}
};

0 comments on commit 8207243

Please sign in to comment.