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

Commit 8fe89d5

Browse files
author
hufeng
committed
添加binarytree例题
1 parent 244bc97 commit 8fe89d5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

‎data_structure/binarytree.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,24 @@ func swap(nums []int, i, j int) {
241241
nums[i] = nums[j]
242242
nums[j] = t
243243
}
244+
245+
/*
246+
常见题目示例
247+
*/
248+
249+
// 给定一个二叉树,找出去最大深度
250+
func maxDepth(root *TreeNode) int {
251+
// 返回条件处理
252+
if root == nil {
253+
return 0
254+
}
255+
// divide:分左右子树分别计算
256+
left := maxDepth(root.Left)
257+
right := maxDepth(root.Right)
258+
259+
// conquer:合并左右子树结果
260+
if left > right {
261+
return left + 1
262+
}
263+
return right + 1
264+
}

‎data_structure/binarytree_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ func TestQuickSort(t *testing.T) {
6969
result := QuickSort(nums)
7070
fmt.Println(result)
7171
}
72+
73+
func TestMaxDepth(t *testing.T) {
74+
result := maxDepth(treeNode)
75+
fmt.Println(result)
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /