Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
GreyZeng
algorithm
提交
6e781f08
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,发现更多精彩内容 >>
提交
6e781f08
编写于
12月 07, 2022
作者:
GreyZeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
34f101c8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
64 addition
and
63 deletion
+64
-63
src/main/java/leetcode/easy/LeetCode_0111_MinimumDepthOfBinaryTree.java
...leetcode/easy/LeetCode_0111_MinimumDepthOfBinaryTree.java
+64
-63
未找到文件。
src/main/java/leetcode/easy/LeetCode_0111_MinimumDepthOfBinaryTree.java
浏览文件 @
6e781f08
package
leetcode.easy
;
//给定一个二叉树,找出其最小深度。
// 笔记:
// 给定一个二叉树,找出其最小深度。
//
// 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
//
...
...
@@ -10,68 +11,68 @@ package leetcode.easy;
// Morris遍历
public
class
LeetCode_0111_MinimumDepthOfBinaryTree
{
public
class
TreeNode
{
TreeNode
left
;
TreeNode
right
;
}
public
class
TreeNode
{
TreeNode
left
;
TreeNode
right
;
}
// 二叉树的递归套路
public
static
int
minDepth
(
TreeNode
head
)
{
if
(
head
==
null
)
{
return
0
;
}
if
(
head
.
left
==
null
)
{
return
minDepth
(
head
.
right
)
+
1
;
}
if
(
head
.
right
==
null
)
{
return
minDepth
(
head
.
left
)
+
1
;
}
return
Math
.
min
(
minDepth
(
head
.
left
),
minDepth
(
head
.
right
))
+
1
;
}
// 二叉树的递归套路
public
static
int
minDepth
(
TreeNode
head
)
{
if
(
head
==
null
)
{
return
0
;
}
if
(
head
.
left
==
null
)
{
return
minDepth
(
head
.
right
)
+
1
;
}
if
(
head
.
right
==
null
)
{
return
minDepth
(
head
.
left
)
+
1
;
}
return
Math
.
min
(
minDepth
(
head
.
left
),
minDepth
(
head
.
right
))
+
1
;
}
// morris遍历方式
public
static
int
minDepth2
(
TreeNode
head
)
{
if
(
head
==
null
)
{
return
0
;
}
TreeNode
cur
=
head
;
TreeNode
mostRight
;
int
curHeight
=
0
;
int
min
=
Integer
.
MAX_VALUE
;
while
(
cur
!=
null
)
{
mostRight
=
cur
.
left
;
if
(
mostRight
!=
null
)
{
int
dulplicate
=
1
;
while
(
mostRight
.
right
!=
null
&&
mostRight
.
right
!=
cur
)
{
dulplicate
++;
mostRight
=
mostRight
.
right
;
}
if
(
mostRight
.
right
==
null
)
{
curHeight
++;
mostRight
.
right
=
cur
;
cur
=
cur
.
left
;
continue
;
}
else
{
if
(
mostRight
.
left
==
null
)
{
min
=
Math
.
min
(
min
,
curHeight
);
}
curHeight
-=
dulplicate
;
mostRight
.
right
=
null
;
}
}
else
{
curHeight
++;
}
cur
=
cur
.
right
;
}
int
rightMostHeight
=
1
;
TreeNode
c
=
head
;
while
(
c
.
right
!=
null
)
{
rightMostHeight
++;
c
=
c
.
right
;
}
if
(
c
.
left
==
null
)
{
min
=
Math
.
min
(
min
,
rightMostHeight
);
}
return
min
;
}
// morris遍历方式
public
static
int
minDepth2
(
TreeNode
head
)
{
if
(
head
==
null
)
{
return
0
;
}
TreeNode
cur
=
head
;
TreeNode
mostRight
;
int
curHeight
=
0
;
int
min
=
Integer
.
MAX_VALUE
;
while
(
cur
!=
null
)
{
mostRight
=
cur
.
left
;
if
(
mostRight
!=
null
)
{
int
dulplicate
=
1
;
while
(
mostRight
.
right
!=
null
&&
mostRight
.
right
!=
cur
)
{
dulplicate
++;
mostRight
=
mostRight
.
right
;
}
if
(
mostRight
.
right
==
null
)
{
curHeight
++;
mostRight
.
right
=
cur
;
cur
=
cur
.
left
;
continue
;
}
else
{
if
(
mostRight
.
left
==
null
)
{
min
=
Math
.
min
(
min
,
curHeight
);
}
curHeight
-=
dulplicate
;
mostRight
.
right
=
null
;
}
}
else
{
curHeight
++;
}
cur
=
cur
.
right
;
}
int
rightMostHeight
=
1
;
TreeNode
c
=
head
;
while
(
c
.
right
!=
null
)
{
rightMostHeight
++;
c
=
c
.
right
;
}
if
(
c
.
left
==
null
)
{
min
=
Math
.
min
(
min
,
rightMostHeight
);
}
return
min
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录