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 4f1c6de

Browse files
author
Shuo
authored
Merge pull request #406 from openset/develop
Add: Maximum Depth of Binary Tree
2 parents 76a24bf + 39f200b commit 4f1c6de

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
package maximum_depth_of_binary_tree
2+
3+
import . "github.com/openset/leetcode/internal/kit"
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* type TreeNode struct {
8+
* Val int
9+
* Left *TreeNode
10+
* Right *TreeNode
11+
* }
12+
*/
13+
func maxDepth(root *TreeNode) int {
14+
if root == nil {
15+
return 0
16+
}
17+
l := maxDepth(root.Left) + 1
18+
r := maxDepth(root.Right) + 1
19+
if l > r {
20+
return l
21+
}
22+
return r
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
package maximum_depth_of_binary_tree
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/openset/leetcode/internal/kit"
7+
)
8+
9+
type caseType struct {
10+
input []int
11+
expected int
12+
}
13+
14+
func TestMaxDepth(t *testing.T) {
15+
tests := [...]caseType{
16+
{
17+
input: []int{3, 9, 20, NULL, NULL, 15, 7},
18+
expected: 3,
19+
},
20+
{
21+
input: []int{1, 2, 3, NULL, 5, 6},
22+
expected: 3,
23+
},
24+
{
25+
input: []int{1, 2, NULL, NULL, 5},
26+
expected: 3,
27+
},
28+
{
29+
input: []int{1, NULL, 3, NULL, 5},
30+
expected: 3,
31+
},
32+
}
33+
for _, tc := range tests {
34+
output := maxDepth(SliceInt2TreeNode(tc.input))
35+
if output != tc.expected {
36+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
37+
}
38+
}
39+
}

0 commit comments

Comments
(0)

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