题目:
题解:
class Solution:def postorderTraversal(self, root: TreeNode) -> List[int]:def addPath(node: TreeNode):count = 0while node:count += 1res.append(node.val)node = node.righti, j = len(res) - count, len(res) - 1while i < j:res[i], res[j] = res[j], res[i]i += 1j -= 1if not root:return list()res = list()p1 = rootwhile p1:p2 = p1.leftif p2:while p2.right and p2.right != p1:p2 = p2.rightif not p2.right:p2.right = p1p1 = p1.leftcontinueelse:p2.right = NoneaddPath(p1.left)p1 = p1.rightaddPath(root)return res