思路
二叉树的遍历
code
func tree2str(t *TreeNode) string {
if t == nil {
return ""
}
if t.Left == nil && t.Right == nil {
return strconv.Itoa(t.Val)
}
if t.Right == nil {
return strconv.Itoa(t.Val) + "(" + tree2str(t.Left) + ")"
}
return strconv.Itoa(t.Val) + "(" + tree2str(t.Left) + ")(" + tree2str(t.Right) + ")"
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode