Skip to content

Commit cdac5f7

Browse files
committed
1 parent 0c43847 commit cdac5f7

File tree

1 file changed

+25
-0
lines changed
  • src/main/kotlin/leetcode/inorder-successor-in-bst-ii

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode.`inorder-successor-in-bst-ii`
2+
3+
import TreeNode
4+
5+
class Solution {
6+
fun inorderSuccessor(node: Node?): Node? {
7+
node ?: return null
8+
var explore: Node?
9+
return if (node.right == null) {
10+
explore = node
11+
while (explore != null && explore == explore.parent?.right) explore = explore.parent
12+
explore?.parent
13+
} else {
14+
explore = node.right
15+
while (explore?.left != null) explore = explore.left
16+
explore
17+
}
18+
}
19+
20+
class Node(var `val`: Int) {
21+
var left: Node? = null
22+
var right: Node? = null
23+
var parent: Node? = null
24+
}
25+
}

0 commit comments

Comments
 (0)