Skip to content

Commit 92b23a4

Browse files
committed
285 (2) update iterative solution
1 parent bbfc235 commit 92b23a4

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

src/_285_InorderSuccessorInBST/Solution.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.leetcode.TreeNode;
2020

2121
/** see test {@link _285_InorderSuccessorInBST.SolutionTest } */
22-
2322
public class Solution {
2423

2524
// iterative version
@@ -36,10 +35,8 @@ public TreeNode inorderSuccessor(TreeNode root, TreeNode node) {
3635
} else {
3736
// find successor from root down to this node
3837
TreeNode successor = null;
39-
while (root != null) {
40-
if (root == node) {
41-
break;
42-
} else if (node.val < root.val) {
38+
while (root != null && root != node) {
39+
if (node.val < root.val) {
4340
// go to left subtree
4441
successor = root;
4542
root = root.left;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy