Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49cfd42

Browse files
MEDIUM/src/medium/InorderSuccessorInBST.java
1 parentcb66901 commit49cfd42

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagemedium;
2+
3+
importclasses.TreeNode;
4+
5+
/**285. Inorder Successor in BST
6+
7+
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
8+
9+
Note: If the given node has no in-order successor in the tree, return null. */
10+
publicclassInorderSuccessorInBST {
11+
12+
//Amazed at this solution: https://discuss.leetcode.com/topic/25698/java-python-solution-o-h-time-and-o-1-space-iterative
13+
//I used brute force: traverse the tree and store and nodes in a list, then traverse the list to compare with p to return the successor if there is.
14+
publicTreeNodeinorderSuccessor(TreeNoderoot,TreeNodep) {
15+
TreeNodesucc =null;
16+
while(root !=null){
17+
if(p.val <root.val){
18+
succ =root;
19+
root =root.left;
20+
}else {
21+
root =root.right;
22+
}
23+
}
24+
returnsucc;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp