Expert Beginner

Learning by doing

0%

Leetcode 94 Binary Tree Inorder Traversal

個人覺得這題應該是easy,不過不重要。今天在710往永寧站的車上想說來試一下在手機瀏覽器leetcode用Go寫一題練習看看。感想是自己Go程式還是寫的不夠多,感覺還沒出來。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func inorder(root *TreeNode, res *[]int) {
if root == nil {
return
}
inorder(root.Left, res)
*res = append(*res, root.Val)
inorder(root.Right, res)
}

func inorderTraversal(root *TreeNode) []int {
var res []int
inorder(root, &res)
return res
}