// 辅助函数:打印链表
func printList(root *TreeNode) {
if root == nil {
fmt.Println("[]")
return
}
curr := root
for curr != nil {
fmt.Printf("%d", curr.Val)
if curr.Right != nil {
fmt.Print("->")
}
curr = curr.Right
}
fmt.Println()
}
func main() {
// 示例1: [1,2,5,3,4,null,6]
root := &TreeNode{1,
&TreeNode{2,
&TreeNode{3, nil, nil},
&TreeNode{4, nil, nil},
},
&TreeNode{5,
nil,
&TreeNode{6, nil, nil},
},
}
flatten(root)
printList(root) // 输出: 1->2->3->4->5->6
// 示例2: 空树
var root2 *TreeNode
flatten(root2)
printList(root2) // 输出: []
// 示例3: 单节点
root3 := &TreeNode{0, nil, nil}
flatten(root3)
printList(root3) // 输出: 0
}