Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
GreyZeng
algorithm
提交
772bc30c
A
algorithm
项目概览
GreyZeng
/
algorithm
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
772bc30c
编写于
11月 06, 2022
作者:
GreyZeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
in order successor in bst
上级
b835d403
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
134 addition
and
98 deletion
+134
-98
src/main/java/lintcode/medium/LintCode_0448_InorderSuccessorInBST.java
.../lintcode/medium/LintCode_0448_InorderSuccessorInBST.java
+133
-97
src/main/java/snippet/Code_MakeCoffee.java
src/main/java/snippet/Code_MakeCoffee.java
+1
-1
未找到文件。
src/main/java/lintcode/medium/LintCode_0448_InorderSuccessorInBST.java
浏览文件 @
772bc30c
package
lintcode.medium
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// leetcode 285
// https://www.lintcode.com/problem/inorder-successor-in-bst/description
public
class
LintCode_0448_InorderSuccessorInBST
{
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
)
{
val
=
x
;
}
}
public
static
TreeNode
inorderSuccessor
(
TreeNode
root
,
TreeNode
p
)
{
if
(
p
==
null
)
{
return
null
;
}
if
(
p
.
right
!=
null
)
{
return
rightLeftMost
(
p
.
right
);
}
TreeNode
sucessor
=
null
;
while
(
root
!=
null
)
{
if
(
root
.
val
>
p
.
val
)
{
sucessor
=
root
;
root
=
root
.
left
;
}
else
if
(
root
.
val
<
p
.
val
)
{
root
=
root
.
right
;
}
else
{
break
;
}
}
return
sucessor
;
}
private
static
TreeNode
rightLeftMost
(
TreeNode
p
)
{
while
(
p
.
left
!=
null
)
{
p
=
p
.
left
;
}
return
p
;
}
public
static
TreeNode
inorderSuccessor2
(
TreeNode
root
,
TreeNode
p
)
{
List
<
TreeNode
>
ans
=
new
ArrayList
<>();
if
(
root
==
null
)
{
return
null
;
}
in2
(
root
,
ans
);
boolean
find
=
false
;
for
(
TreeNode
c
:
ans
)
{
if
(
c
==
p
)
{
find
=
true
;
}
else
if
(
find
)
{
return
c
;
}
}
return
null
;
}
private
static
void
in2
(
TreeNode
root
,
List
<
TreeNode
>
ans
)
{
if
(
root
==
null
)
{
return
;
}
in2
(
root
.
left
,
ans
);
ans
.
add
(
root
);
in2
(
root
.
right
,
ans
);
}
public
TreeNode
inorderSuccessor3
(
TreeNode
root
,
TreeNode
p
)
{
if
(
root
==
null
)
{
return
null
;
}
boolean
flag
=
false
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
TreeNode
cur
=
root
;
while
(!
stack
.
isEmpty
()
||
cur
!=
null
)
{
if
(
cur
!=
null
)
{
stack
.
push
(
cur
);
cur
=
cur
.
left
;
}
else
{
cur
=
stack
.
pop
();
if
(
cur
==
p
)
{
flag
=
true
;
}
else
if
(
flag
)
{
return
cur
;
}
cur
=
cur
.
right
;
}
}
return
null
;
}
}
package
lintcode.medium
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// 笔记:https://www.cnblogs.com/greyzeng/p/16863345.html
// 二叉搜索树中查找后继节点
// leetcode 285
// https://www.lintcode.com/problem/inorder-successor-in-bst/description
public
class
LintCode_0448_InorderSuccessorInBST
{
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
)
{
val
=
x
;
}
}
public
static
TreeNode
inorderSuccessor
(
TreeNode
root
,
TreeNode
p
)
{
if
(
p
==
null
)
{
return
null
;
}
if
(
p
.
right
!=
null
)
{
return
rightLeftMost
(
p
.
right
);
}
TreeNode
successor
=
null
;
while
(
root
!=
null
)
{
if
(
root
.
val
>
p
.
val
)
{
successor
=
root
;
root
=
root
.
left
;
}
else
if
(
root
.
val
<
p
.
val
)
{
root
=
root
.
right
;
}
else
{
break
;
}
}
return
successor
;
}
private
static
TreeNode
rightLeftMost
(
TreeNode
p
)
{
while
(
p
.
left
!=
null
)
{
p
=
p
.
left
;
}
return
p
;
}
public
static
TreeNode
inorderSuccessor2
(
TreeNode
root
,
TreeNode
p
)
{
List
<
TreeNode
>
ans
=
new
ArrayList
<>();
if
(
root
==
null
)
{
return
null
;
}
in2
(
root
,
ans
);
boolean
find
=
false
;
for
(
TreeNode
c
:
ans
)
{
if
(
c
==
p
)
{
find
=
true
;
}
else
if
(
find
)
{
return
c
;
}
}
return
null
;
}
private
static
void
in2
(
TreeNode
root
,
List
<
TreeNode
>
ans
)
{
if
(
root
==
null
)
{
return
;
}
in2
(
root
.
left
,
ans
);
ans
.
add
(
root
);
in2
(
root
.
right
,
ans
);
}
public
TreeNode
inorderSuccessor4
(
TreeNode
head
,
TreeNode
p
)
{
if
(
head
==
null
)
{
return
null
;
}
TreeNode
ans
=
null
;
TreeNode
cur
=
head
;
TreeNode
mostRight
;
boolean
find
=
false
;
while
(
cur
!=
null
)
{
mostRight
=
cur
.
left
;
if
(
mostRight
!=
null
)
{
while
(
mostRight
.
right
!=
null
&&
mostRight
.
right
!=
cur
)
{
mostRight
=
mostRight
.
right
;
}
if
(
mostRight
.
right
==
null
)
{
mostRight
.
right
=
cur
;
cur
=
cur
.
left
;
continue
;
}
else
{
mostRight
.
right
=
null
;
}
}
if
(
find
)
{
ans
=
cur
;
find
=
false
;
}
if
(
cur
==
p
)
{
find
=
true
;
}
cur
=
cur
.
right
;
}
return
ans
;
}
public
TreeNode
inorderSuccessor3
(
TreeNode
root
,
TreeNode
p
)
{
if
(
root
==
null
)
{
return
null
;
}
boolean
flag
=
false
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
TreeNode
cur
=
root
;
while
(!
stack
.
isEmpty
()
||
cur
!=
null
)
{
if
(
cur
!=
null
)
{
stack
.
push
(
cur
);
cur
=
cur
.
left
;
}
else
{
cur
=
stack
.
pop
();
if
(
cur
==
p
)
{
flag
=
true
;
}
else
if
(
flag
)
{
return
cur
;
}
cur
=
cur
.
right
;
}
}
return
null
;
}
}
src/main/java/snippet/Code_
0085_
Coffee.java
→
src/main/java/snippet/Code_
Make
Coffee.java
浏览文件 @
772bc30c
...
...
@@ -17,7 +17,7 @@ import java.util.PriorityQueue;
// 思路:
// 第一步:先想办法得到一个数组,获取每个人喝咖啡的最少时间数组(小根堆 能用时间+多久制作的时间)
// 第二步:process(i,free) 从i号咖啡杯开始,一直到最后,最短时间是?其中free是咖啡杯可用的时间
public
class
Code_
0085_
Coffee
{
public
class
Code_
Make
Coffee
{
public
static
int
minTime0
(
int
[]
arr
,
int
n
,
int
wash
,
int
air
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录