Skip to content

Commit 925fcc4

Browse files
committed
feat(solutions): add binary_tree_inorder_traversal
1 parent 7135ec8 commit 925fcc4

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
3434
| # | **Problem** & **Solution** | Difficulty | Single Repetition Duration | LeetCode Run Time |
3535
| ---: | :----- | :----------: | ----------: | ----------: |
36+
|[94][Solutions-94]|[Binary Tree Inorder Traversal][Solutions-94-Home]|Medium|[119 ns/op][Solutions-94-Code] / [3 test cases][Solutions-94-Test]|0 ms|
3637
|[93][Solutions-93]|[Restore IP Addresses][Solutions-93-Home]|Medium|[251 ns/op][Solutions-93-Code] / [4 test cases][Solutions-93-Test]|4 ms|
3738
|[92][Solutions-92]|[Reverse Linked List II][Solutions-92-Home]|Medium|[61.5 ns/op][Solutions-92-Code] / [3 test cases][Solutions-92-Test]|3 ms|
3839
|[91][Solutions-91]|[Decode Ways][Solutions-91-Home]|Medium|[68.3 ns/op][Solutions-91-Code] / [8 test cases][Solutions-91-Test]|3 ms|
@@ -149,6 +150,10 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
149150
### Support
150151
If you like it then you can put a :star:Star on it.
151152

153+
[Solutions-94]:https://leetcode.com/problems/binary-tree-inorder-traversal/
154+
[Solutions-94-Home]:solutions/binary_tree_inorder_traversal/
155+
[Solutions-94-Code]:solutions/binary_tree_inorder_traversal/binarytreeinordertraversal.go
156+
[Solutions-94-Test]:solutions/binary_tree_inorder_traversal/binarytreeinordertraversal_test.go#L49
152157
[Solutions-93]:https://leetcode.com/problems/restore-ip-addresses/
153158
[Solutions-93-Home]:solutions/restore_ip_addresses/
154159
[Solutions-93-Code]:solutions/restore_ip_addresses/restoreipaddresses.go

solutions/binary_tree_inorder_traversal/NOTE_Ch-zh.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# [94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/description/)
2+
3+
## Description
4+
5+
Given a binary tree, return the _inorder_ traversal of its nodes' values.
6+
7+
For example:
8+
9+
Given binary tree `[1,null,2,3]`,
10+
```
11+
1
12+
\
13+
2
14+
/
15+
3
16+
```
17+
18+
return `[1,3,2]`.
19+
20+
**Note:** Recursive solution is trivial, could you do it iteratively?
21+
22+
## Solution
23+
- [Code](binarytreeinordertraversal.go)
24+
- [Testing](binarytreeinordertraversal_test.go)
25+
26+
## Note
27+
- [中文](NOTE_Ch-zh.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package binarytreeinordertraversal
2+
3+
// TreeNode definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
func inorderTraversal(root *TreeNode) []int {
11+
var result []int
12+
if root != nil {
13+
// push all the left children from root node.
14+
result = append(result, inorderTraversal(root.Left)...)
15+
// push the root node
16+
result = append(result, root.Val)
17+
// push all the right children from root node.
18+
result = append(result, inorderTraversal(root.Right)...)
19+
}
20+
return result
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package binarytreeinordertraversal
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_inorderTraversal(t *testing.T) {
10+
assert.Empty(t, inorderTraversal(nil))
11+
assert.Equal(t,
12+
[]int{1},
13+
inorderTraversal(&TreeNode{
14+
Val: 1,
15+
}))
16+
assert.Equal(t,
17+
[]int{1, 3, 2},
18+
inorderTraversal(&TreeNode{
19+
Val: 1,
20+
Right: &TreeNode{
21+
Val: 2,
22+
Left: &TreeNode{
23+
Val: 3,
24+
},
25+
},
26+
}))
27+
assert.Equal(t,
28+
[]int{5, 3, 1, 2, 4, 6},
29+
inorderTraversal(&TreeNode{
30+
Val: 1,
31+
Left: &TreeNode{
32+
Val: 3,
33+
Left: &TreeNode{
34+
Val: 5,
35+
},
36+
},
37+
Right: &TreeNode{
38+
Val: 2,
39+
Right: &TreeNode{
40+
Val: 4,
41+
Right: &TreeNode{
42+
Val: 6,
43+
},
44+
},
45+
},
46+
}))
47+
}
48+
49+
func Benchmark_inorderTraversal(b *testing.B) {
50+
b.StopTimer()
51+
b.ReportAllocs()
52+
b.StartTimer()
53+
b.RunParallel(func(pb *testing.PB) {
54+
for pb.Next() {
55+
inorderTraversal(nil)
56+
inorderTraversal(&TreeNode{
57+
Val: 1,
58+
Right: &TreeNode{
59+
Val: 2,
60+
Left: &TreeNode{
61+
Val: 3,
62+
},
63+
},
64+
})
65+
inorderTraversal(&TreeNode{
66+
Val: 1,
67+
Left: &TreeNode{
68+
Val: 3,
69+
Left: &TreeNode{
70+
Val: 5,
71+
},
72+
},
73+
Right: &TreeNode{
74+
Val: 2,
75+
Right: &TreeNode{
76+
Val: 4,
77+
Right: &TreeNode{
78+
Val: 6,
79+
},
80+
},
81+
},
82+
})
83+
}
84+
})
85+
}

0 commit comments

Comments
 (0)