Skip to content

Commit 2ff0a0a

Browse files
committed
Add: 235LowestCommonAncestorofaBinarySearchTree iteration Solution
1 parent dd55d87 commit 2ff0a0a

File tree

1 file changed

+104
-50
lines changed

1 file changed

+104
-50
lines changed

235LowestCommonAncestorofaBinarySearchTree.org

+104-50
Original file line numberDiff line numberDiff line change
@@ -27,59 +27,113 @@
2727
二分搜索树的特点是,左子树小于根节点,右子树大于根节点。
2828

2929
如果给出的两个节点都小于根节点,那么最近的公共祖先在左子树中。如果给出的两个节点都大于根节点,那么最近的公共祖先在右子树中。如果两个节点有一个小于根节点另一个大于根节点那么该节点就是最近公共祖先。
30-
** Java
31-
#+begin_src java
32-
/**
33-
,* Definition for a binary tree node.
34-
,* public class TreeNode {
35-
,* int val;
36-
,* TreeNode left;
37-
,* TreeNode right;
38-
,* TreeNode(int x) { val = x; }
39-
,* }
40-
,*/
41-
class Solution {
42-
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
43-
if (root == null) return null;
30+
** 递归
31+
*** Java
32+
#+begin_src java
33+
/**
34+
,* Definition for a binary tree node.
35+
,* public class TreeNode {
36+
,* int val;
37+
,* TreeNode left;
38+
,* TreeNode right;
39+
,* TreeNode(int x) { val = x; }
40+
,* }
41+
,*/
42+
class Solution {
43+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
44+
if (root == null) return null;
4445

45-
if (root.val > p.val && root.val > q.val) {
46-
return lowestCommonAncestor(root.left, p, q);
47-
}
46+
if (root.val > p.val && root.val > q.val) {
47+
return lowestCommonAncestor(root.left, p, q);
48+
}
4849

49-
if (root.val < p.val && root.val < q.val) {
50-
return lowestCommonAncestor(root.right, p, q);
51-
}
50+
if (root.val < p.val && root.val < q.val) {
51+
return lowestCommonAncestor(root.right, p, q);
52+
}
5253

53-
return root;
54-
}
55-
}
56-
#+end_src
57-
** CPP
58-
#+begin_src cpp
59-
/**
60-
,* Definition for a binary tree node.
61-
,* struct TreeNode {
62-
,* int val;
63-
,* TreeNode *left;
64-
,* TreeNode *right;
65-
,* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
66-
,* };
67-
,*/
68-
class Solution {
69-
public:
70-
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
71-
if (root == NULL) return NULL;
54+
return root;
55+
}
56+
}
57+
#+end_src
58+
*** CPP
59+
#+begin_src cpp
60+
/**
61+
,* Definition for a binary tree node.
62+
,* struct TreeNode {
63+
,* int val;
64+
,* TreeNode *left;
65+
,* TreeNode *right;
66+
,* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
67+
,* };
68+
,*/
69+
class Solution {
70+
public:
71+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
72+
if (root == NULL) return NULL;
7273

73-
if (p->val < root->val && q->val < root->val) {
74-
return lowestCommonAncestor(root->left, p, q);
75-
}
74+
if (p->val < root->val && q->val < root->val) {
75+
return lowestCommonAncestor(root->left, p, q);
76+
}
7677

77-
if (p->val > root->val && q->val > root->val) {
78-
return lowestCommonAncestor(root->right, p, q);
79-
}
78+
if (p->val > root->val && q->val > root->val) {
79+
return lowestCommonAncestor(root->right, p, q);
80+
}
8081

81-
return root;
82-
}
83-
};
84-
#+end_src
85-
82+
return root;
83+
}
84+
};
85+
#+end_src
86+
** 迭代
87+
*** Java
88+
#+begin_src java
89+
/**
90+
,* Definition for a binary tree node.
91+
,* public class TreeNode {
92+
,* int val;
93+
,* TreeNode left;
94+
,* TreeNode right;
95+
,* TreeNode(int x) { val = x; }
96+
,* }
97+
,*/
98+
class Solution {
99+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
100+
while (root != null) {
101+
if (root.val > p.val && root.val > q.val) {
102+
root = root.left;
103+
} else if (root.val < p.val && root.val < q.val) {
104+
root = root.right;
105+
} else {
106+
return root;
107+
}
108+
}
109+
return null;
110+
}
111+
}
112+
#+end_src
113+
*** CPP
114+
#+begin_src cpp
115+
/**
116+
,* Definition for a binary tree node.
117+
,* struct TreeNode {
118+
,* int val;
119+
,* TreeNode *left;
120+
,* TreeNode *right;
121+
,* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
122+
,* };
123+
,*/
124+
class Solution {
125+
public:
126+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
127+
while (root != nullptr) {
128+
if (root->val > q->val && root->val > p->val) {
129+
root = root->left;
130+
} else if (root->val < q->val && root->val < p->val) {
131+
root = root->right;
132+
} else {
133+
return root;
134+
}
135+
}
136+
return nullptr;
137+
}
138+
};
139+
#+end_src

0 commit comments

Comments
 (0)