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 a0e9e1a

Browse files
authored
144 solved. (#47)
1 parent 63f193e commit a0e9e1a

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ continually updating 😃.
9595
* [107. Binary Tree Level Order Traversal II](./src/0107_binary_tree_level_order_traversal_2/binary_tree_level_order_traversal2.go)   *`binary tree;`*   *`bfs`*
9696
* [111. Minimum Depth of Binary Tree](./src/0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)   *`binary tree;`*   *`dfs`*
9797
* [112. Path Sum](./src/0112_path_sum/path_sum.go)   *`binary tree;`*   *`dfs`*
98+
* [144. Binary Tree Preorder Traversal](src/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)   *`binary tree;`*   *`pre-order traversal`*
9899
* [226. Invert Binary Tree](./src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree;`*
99100

100101
### Binary Search
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
144. Binary Tree Preorder Traversal
3+
https://leetcode.com/problems/binary-tree-preorder-traversal/
4+
5+
Given a binary tree, return the preorder traversal of its nodes' values.
6+
*/
7+
// time: 2019年01月06日
8+
9+
package btpot
10+
11+
// TreeNode Definition for a binary tree node.
12+
type TreeNode struct {
13+
Val int
14+
Left *TreeNode
15+
Right *TreeNode
16+
}
17+
18+
// recursive
19+
// time complexity: O(n), where n is nodes numbers in the tree.
20+
// space complexity: O(h), where h is the height of the tree.
21+
func preorderTraversal(root *TreeNode) []int {
22+
if root == nil {
23+
return []int{}
24+
}
25+
26+
if root.Left == nil && root.Right == nil {
27+
return []int{root.Val}
28+
}
29+
30+
left := preorderTraversal(root.Left)
31+
right := preorderTraversal(root.Right)
32+
33+
res := []int{root.Val}
34+
res = append(res, left...)
35+
res = append(res, right...)
36+
return res
37+
}
38+
39+
// iterative
40+
// time complexity: O(n), where n is nodes numbers in the tree.
41+
// space complexity: O(h), where h is the height of the tree.
42+
func preorderTraversal1(root *TreeNode) []int {
43+
if root == nil {
44+
return []int{}
45+
}
46+
47+
var stack []*TreeNode
48+
var res []int
49+
stack = append(stack, root)
50+
51+
for len(stack) > 0 {
52+
node := stack[len(stack)-1]
53+
stack = stack[:len(stack)-1]
54+
55+
res = append(res, node.Val)
56+
57+
if node.Right != nil {
58+
stack = append(stack, node.Right)
59+
}
60+
61+
if node.Left != nil {
62+
stack = append(stack, node.Left)
63+
}
64+
}
65+
return res
66+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package btpot
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func createBinaryTree(nums []int) *TreeNode {
9+
return performCreate(nums, 0)
10+
}
11+
12+
func performCreate(nums []int, index int) *TreeNode {
13+
if index >= len(nums) {
14+
return nil
15+
}
16+
17+
tree := TreeNode{Val: nums[index]}
18+
tree.Left = performCreate(nums, 2*index+1)
19+
tree.Right = performCreate(nums, 2*index+2)
20+
return &tree
21+
}
22+
23+
func TestPreorderTraversal(t *testing.T) {
24+
testCases := []*TreeNode{
25+
nil,
26+
createBinaryTree([]int{1, 2, 3, 4, 5, 6, 7, 8}),
27+
}
28+
expected := [][]int{
29+
{},
30+
{1, 2, 4, 8, 5, 3, 6, 7},
31+
}
32+
testFuncs := []func(node *TreeNode) []int{
33+
preorderTraversal,
34+
preorderTraversal1,
35+
}
36+
37+
for _, testFunc := range testFuncs {
38+
for index, root := range testCases {
39+
if res := testFunc(root); !reflect.DeepEqual(res, expected[index]) {
40+
t.Errorf("expected %v, got %v", expected[index], res)
41+
}
42+
}
43+
}
44+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
|0121|[121. Best Time to Buy and Sell Stock](0121_best_time_to_buy_and_sell_stock/maxprofit.go)|Easy||
5858
|0122|[122. Best Time to Buy and Sell Stock II](0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)|Easy|*`greedy`*|
5959
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
60+
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
6061
|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|
6162
|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|
6263
|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|

0 commit comments

Comments
(0)

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