Skip to content

Commit fb44631

Browse files
committed
0235 new algo added.
1 parent 0f1f3e2 commit fb44631

File tree

6 files changed

+82
-12
lines changed

6 files changed

+82
-12
lines changed

0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/cpp-0235/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ class Solution {
2424
public:
2525
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
2626

27-
assert(p != NULL && q != NULL);
27+
assert(!p && !q);
2828

29-
if(root == NULL)
30-
return NULL;
29+
if(!root) return root;
3130

3231
if(p->val < root->val && q->val < root->val)
3332
return lowestCommonAncestor(root->left, p, q);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-12-16
4+
5+
#include <iostream>
6+
#include <cassert>
7+
8+
using namespace std;
9+
10+
11+
/// Definition for a binary tree node.
12+
struct TreeNode {
13+
int val;
14+
TreeNode *left;
15+
TreeNode *right;
16+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
17+
};
18+
19+
20+
/// Non-Recursive
21+
/// Time Complexity: O(lgn), where n is the node's number of the tree
22+
/// Space Complexity: O(1)
23+
class Solution {
24+
public:
25+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
26+
27+
if(!root) return root;
28+
29+
TreeNode* cur = root;
30+
while(cur) {
31+
if (p->val < cur->val && q->val < cur->val)
32+
cur = cur->left;
33+
else if (p->val > cur->val && q->val > cur->val)
34+
cur = cur->right;
35+
else
36+
return cur;
37+
}
38+
39+
return NULL;
40+
}
41+
};
42+
43+
int main() {
44+
45+
return 0;
46+
}

0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/java-0235/src/Solution.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
/// Space Complexity: O(h), where h is the height of the tree
88
class Solution {
99

10-
// Definition for a binary tree node.
11-
public class TreeNode {
12-
int val;
13-
TreeNode left;
14-
TreeNode right;
15-
TreeNode(int x) { val = x; }
16-
}
17-
1810
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
1911

2012
if(p == null || q == null)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// Source : https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-12-16
4+
5+
/// Non-Recursive
6+
/// Time Complexity: O(lgn), where n is the node's number of the tree
7+
/// Space Complexity: O(1)
8+
class Solution2 {
9+
10+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
11+
12+
if(root == null)
13+
return root;
14+
15+
TreeNode cur = root;
16+
while(cur != null){
17+
if(p.val < cur.val && q.val < cur.val)
18+
cur = cur.left;
19+
else if(p.val > cur.val && q.val > cur.val)
20+
cur = cur.right;
21+
else
22+
return cur;
23+
}
24+
return null;
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Definition for a binary tree node.
2+
class TreeNode {
3+
int val;
4+
TreeNode left;
5+
TreeNode right;
6+
TreeNode(int x) { val = x; }
7+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
238238
| 232 | [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/description/) | [solution](https://leetcode.com/problems/implement-queue-using-stacks/solution/) | [C++](0232-Implement-Queue-using-Stacks/cpp-0232/) | | |
239239
| | | | | | |
240240
| 234 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/description/) | [] | [C++](0234-Palindrome-Linked-List/cpp-0234/) | | |
241-
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) | [] | [C++](0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/cpp-0235/) | [Java](0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/java-0235/src/) | |
241+
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) | [solution](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/solution/) | [C++](0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/cpp-0235/) | [Java](0235-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/java-0235/src/) | |
242242
| 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) | [solution](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/)<br/>[缺:非递归解法;LCA问题总结] | [C++](0236-Lowest-Common-Ancestor-of-a-Binary-Tree/cpp-0236/) | | |
243243
| 237 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/description/) | [solution](https://leetcode.com/problems/delete-node-in-a-linked-list/solution/) | [C++](0237-Delete-Node-in-a-Linked-List/cpp-0237/) | [Java](0237-Delete-Node-in-a-Linked-List/java-0237/src/) | |
244244
| | | | | | |

0 commit comments

Comments
 (0)