思路
二叉树的中序遍历,二叉树的基本操作之一
code
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func inorderTraversal(root *TreeNode) []int {
res := []int{}
if root == nil {
return res
}
helper(&res, root)
return res
}
func helper(res *[]int, root *TreeNode) {
if root.Left != nil {
helper(res, root.Left)
}
*res = append(*res, root.Val)
if root.Right != nil {
helper(res, root.Right)
}
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode