Clear2 / Algorithm

算法训练
MIT License
0 stars 0 forks source link

二叉树的中序遍历 #10

Open Clear2 opened 4 years ago

Clear2 commented 4 years ago
func inorderTraverSal(root *TreeNode) []int {
    var result []int
    inorderRecursive(root, &result)
    return result
}

func inorderRecursive(root *TreeNode, output*[]int) {
    if root != nil {
        inorderRecursive(root.Left, output)
        *output = append(*output, root.Val)
        inorderRecursive(root.Right, output)
    }
}
Clear2 commented 4 years ago
/*
    1
     \
      2
     /
    3
*/
func main() {
    node3 := TreeNode{Val: 3}
    node2 := TreeNode{Val: 2, Left: &node3}
    node0 := TreeNode{Val: 0 }
    node1 := TreeNode{Val: 1, Right: &node2, Left: &node0}

    result := inorderTraverSal(&node1)
    fmt.Println(result)
}