题目:
题解:
func preorderTraversal(root *TreeNode) (vals []int) {var p1, p2 *TreeNode = root, nilfor p1 != nil {p2 = p1.Leftif p2 != nil {for p2.Right != nil && p2.Right != p1 {p2 = p2.Right}if p2.Right == nil {vals = append(vals, p1.Val)p2.Right = p1p1 = p1.Leftcontinue}p2.Right = nil} else {vals = append(vals, p1.Val)}p1 = p1.Right}return
}