层次遍历(非递归)
LT102: 给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点
# 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
def levelOrder(root: TreeNode):
#如果root是空,则返回[]
if not root: return []
stack = []
stack.append(root)
ans = []
while stack:
#收集stack里面的每一层元素
ans.append(node.val for node in stack)
#把上一层的数字放入tmp里,然后就清空stack,准备循环再次收录下一层
tmp = stack
stack = []
for node in tmp:
if node.left: stack.append(node.left)
if node.right: stack.append(node.right)
return ans
前序遍历(非递归)
根节点->左子树->右子树
LT144:给你二叉树的根节点 root ,返回它节点值的 前序 遍历
# 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
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
ans = []
if not root: return []
stack = [root]
while stack:
ans.append(root.val)
if root.right: stack.append(root.right)
if root.left: stack.append(root.left)
root = stack.pop()
return ans
前序遍历(递归)
# 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
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
if not root: return []
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)
中序遍历(非递归)
左子树->根节点->右子树
# 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
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
ans = []
stack = []
while root or len(stack)>0:
if root:
stack.append(root)
root = root.left
else:
root = stack.pop()
ans.append(root.val)
root = root.right
return ans
中序遍历(递归)
# 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
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
if not root: return []
return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
后序遍历(非递归)
左子树->右子树->根节点
# 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
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
stack1 = [root]
stack2 = []
ans = []
while stack1:
node = stack1.pop()
stack2.append(node)
if node and node.left: stack1.append(node.left)
if node and node.right: stack1.append(node.right)
while stack2:
ans.append(stack2.pop())
return ans
后序遍历(递归)
# 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
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
if not root: return []
return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val]