# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
# 前序遍历 defpreOreder(self, root): if root: self.traverse_path.append(root.val) preOreder(self.left) preOreder(self.right)
# 中序遍历 definOreder(self, root): if root: preOreder(self.left) self.traverse_path.append(root.val) preOreder(self.right)
# 后序遍历 defpostOreder(self, root): if root: preOreder(self.left) preOreder(self.right) self.traverse_path.append(root.val)
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
# Recursive-1 classSolution: defpreorderTraversal(self, root: TreeNode) -> List[int]: result = [] self.helper(root, result) return result
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
# Solution-1 classSolution1: defpreorderTraversal(self, root: TreeNode) -> List[int]: stack, result = [], [] while stack or root: while root: # 前序遍历-根左右,先拿根 result.append(root.val) # 压栈 stack.append(root) # 拿完根之后拿左儿子 root = root.left # 左儿子拿出来,拿右儿子 node = stack.pop() root = node.right # # 完成 return result
# Solution-2 简化Solution-1 classSolution2: defpreorderTraversal(self, root: TreeNode) -> List[int]: stack, result = [], [] while stack or root: if root: result.append(root.val) stack.append(root) root = root.left else: node = stack.pop() root = node.right return result
# Solution-3 classSolution3: defpreorderTraversal(self, root: TreeNode) -> List[int]: stack, result = [root], [] while stack: # 拿出根 node = stack.pop() if node: # 前序遍历拿出,先拿根的值 result.append(node.val) # 模仿栈,先入后出。后拿右孩子 stack.append(node.right) stack.append(node.left) return result
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
# Recursive-1 classSolution: definorderTraversal(self, root: TreeNode) -> List[int]: result = [] self.helper(root, result) return result
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
# Solution - 1 classSolution: definorderTraversal(self, root: TreeNode) -> List[int]: ifnot root: return stack, result = [], [] while stack or root: while root: stack.append(root) root = root.left node = stack.pop() result.append(node.val) root = node.right return result
# Solution - 2 简化Solution-1 classSolution: definorderTraversal(self, root: TreeNode) -> List[int]: stack, result = [], [] while stack or root: if root: stack.append(root) root = root.left else: node = stack.pop() result.append(node.val) root = node.right return result
# Solution - 3 classSolution2: definorderTraversal(self, root: TreeNode) -> List[int]: stack, result = [], [] while stack or root: if root: stack.append(root) root = root.left else: node = stack.pop() result.append(node.val) root = node.right return result
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
# Recursive-1 classSolution: defpostorderTraversal(self, root: TreeNode) -> List[int]: result = [] self.helper(root, result) return result