题目:
题解:
class Solution:def preorderTraversal(self, root: TreeNode) -> List[int]:res = list()if not root:return resp1 = rootwhile p1:p2 = p1.leftif p2:while p2.right and p2.right != p1:p2 = p2.rightif not p2.right:res.append(p1.val)p2.right = p1p1 = p1.leftcontinueelse:p2.right = Noneelse:res.append(p1.val)p1 = p1.rightreturn res