Skip to content

Commit 21f4aa5

Browse files
committed
Chapter 07 section 06 C++ codes updated. Java codes added.
1 parent 8ba21ff commit 21f4aa5

File tree

3 files changed

+52
-8
lines changed
  • 07-Binary-Tree-and-Recursion
    • Course Code (C++)/06-Lowest-Common-Ancestor-of-a-Binary-Search-Tree
    • Course Code (Java)/06-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/src

3 files changed

+52
-8
lines changed

07-Binary-Tree-and-Recursion/Course Code (C++)/06-Lowest-Common-Ancestor-of-a-Binary-Search-Tree/main.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
using namespace std;
55

6+
/// 235. Lowest Common Ancestor of a Binary Search Tree
7+
/// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
8+
/// 时间复杂度: O(lgn), 其中n为树的节点个数
9+
/// 空间复杂度: O(h), 其中h为树的高度
10+
611
/// Definition for a binary tree node.
712
struct TreeNode {
813
int val;
@@ -15,18 +20,18 @@ class Solution {
1520
public:
1621
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
1722

18-
assert( p != NULL && q != NULL );
23+
assert(p != NULL && q != NULL);
1924

20-
if( root == NULL )
25+
if(root == NULL)
2126
return NULL;
2227

23-
if( p->val < root->val && q->val < root->val )
24-
return lowestCommonAncestor( root->left, p, q);
25-
if( p->val > root->val && q->val > root->val )
26-
return lowestCommonAncestor( root->right, p, q);
28+
if(p->val < root->val && q->val < root->val)
29+
return lowestCommonAncestor(root->left, p, q);
30+
if(p->val > root->val && q->val > root->val)
31+
return lowestCommonAncestor(root->right, p, q);
2732

28-
assert( p->val == root->val || q->val == root->val
29-
|| (root->val-p->val)*(root->val-q->val) < 0 );
33+
assert(p->val == root->val || q->val == root->val
34+
|| (root->val - p->val) * (root->val - q->val) < 0);
3035

3136
return root;
3237
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Main {
2+
3+
public static void main(String[] args) {
4+
// write your code here
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// 235. Lowest Common Ancestor of a Binary Search Tree
2+
/// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
3+
/// 时间复杂度: O(lgn), 其中n为树的节点个数
4+
/// 空间复杂度: O(h), 其中h为树的高度
5+
class Solution {
6+
7+
// Definition for a binary tree node.
8+
public class TreeNode {
9+
int val;
10+
TreeNode left;
11+
TreeNode right;
12+
TreeNode(int x) { val = x; }
13+
}
14+
15+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
16+
17+
if(p == null || q == null)
18+
throw new IllegalArgumentException("p or q can not be null.");
19+
20+
if(root == null)
21+
return null;
22+
23+
if(p.val < root.val && q.val < root.val)
24+
return lowestCommonAncestor(root.left, p, q);
25+
if(p.val > root.val && q.val > root.val)
26+
return lowestCommonAncestor(root.right, p, q);
27+
28+
assert p.val == root.val || q.val == root.val
29+
|| (root.val - p.val) * (root.val - q.val) < 0;
30+
31+
return root;
32+
}
33+
}

0 commit comments

Comments
 (0)