feat: add Lowest Common Ancestor of a Binary Tree (#1155)

* add leetcode Lowest Common Ancestor of a Binary Tree

* Update leetcode/src/236.c

Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>

* fix function nam

* update struct name.

* add comments

* Update leetcode/src/236.c

Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>

Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Alexander Pantyukhin 2022-12-21 18:46:34 +04:00 committed by GitHub
parent 9f1591fbd2
commit 7aae73495f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 0 deletions

View File

@ -66,6 +66,7 @@
| 230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [C](./src/230.c) | Medium |
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [C](./src/231.c) | Easy |
| 234 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | [C](./src/234.c) | Easy |
| 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [C](./src/236.c) | Medium |
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [C](./src/242.c) | Easy |
| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [C](./src/268.c) | Easy |
| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [C](./src/278.c) | Easy |

82
leetcode/src/236.c Normal file
View File

@ -0,0 +1,82 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
// The list for TreeNodes.
struct ListItem {
struct TreeNode* node; // TreeNode pointer
struct ListItem* next; // Pointer to the next ListItem
};
bool findTargetPath(struct TreeNode* node, struct TreeNode* target, struct ListItem* path){
if (node == NULL){
return false;
}
struct ListItem* pathItem = malloc(sizeof(struct ListItem));
pathItem->node = node;
pathItem->next = NULL;
path->next = pathItem;
if (node->val == target->val){
return true;
}
if (findTargetPath(node->left, target, pathItem)){
return true;
}
if (findTargetPath(node->right, target, pathItem)){
return true;
}
path->next = NULL;
free(pathItem);
return false;
}
void freeList(struct ListItem* target){
if (target->next != NULL){
freeList(target->next);
}
free(target);
}
// Find full path for p and q.
// Find the longest common path in paths.
// Runtime: O(n)
// Space: O(n)
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
struct ListItem* pPath = malloc(sizeof(struct ListItem));
struct ListItem* qPath = malloc(sizeof(struct ListItem));
findTargetPath(root, p, pPath);
findTargetPath(root, q, qPath);
struct TreeNode* lowestTreeNode = NULL;
struct ListItem* pPathCursor = pPath->next;
struct ListItem* qPathCursor = qPath->next;
while(pPathCursor != NULL && qPathCursor != NULL) {
if (pPathCursor->node->val == qPathCursor->node->val){
lowestTreeNode = pPathCursor->node;
pPathCursor = pPathCursor->next;
qPathCursor = qPathCursor->next;
continue;
}
break;
}
freeList(pPath);
freeList(qPath);
return lowestTreeNode;
}