We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0315a1a commit 1aa0cf7Copy full SHA for 1aa0cf7
lowest-common-ancestor-of-a-binary-search-tree/sora0319.java
@@ -0,0 +1,15 @@
1
+public class Solution {
2
+ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3
+ TreeNode node = root;
4
+ while (node != null) {
5
+ if (p.val < node.val && q.val < node.val) {
6
+ node = node.left;
7
+ } else if (node.val < p.val && node.val < q.val) {
8
+ node = node.right;
9
+ } else {
10
+ return node;
11
+ }
12
13
+ return null;
14
15
+}
0 commit comments