非递归
class Solution:
def aaa(self, root):
res = []
white, red = 0, 1
stack = [(white, root)]
while stack:
color, node = stack.pop()
if color == white:
### ============== 可替换 =================
stack.append((red, node.val))
stack.append((white, node.right))
stack.append((white, node.left))
#### =====================================
else:
res.append(node.val)
return res
递归
class Solution:
def aaa(self, root):
if not root:
return None
return [root.val] + self.aaa(root.right) + self.aaa(root.left)