树的各种遍历
树的遍历
1.前序遍历(preoOrder)
https://leetcode.com/problems/binary-tree-preorder-traversal/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = []
node = []
if not root:
return res
node.append(root)
while node:
temp = node.pop()
res.append(temp.val)
if temp.right:
node.append(temp.right)
if temp.left:
node.append(temp.left)
return res
# 递归实现
# return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) if root is not None else []
2.中序遍历(inOrder)
https://leetcode.com/problems/binary-tree-inorder-traversal/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
node = []
res = []
if not root:
return res
node.append(root)
temp = root
while node:
while temp and temp.left:
node.append(temp.left)
temp = temp.left
temp = node.pop()
res.append(temp.val)
temp = temp.right
if temp:
node.append(temp)
return res
# 递归实现
#return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right) if root is not None else []
3.后序遍历(postOrder)
https://leetcode.com/problems/binary-tree-postorder-traversal/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
# 反的‘前序遍历’(根->右->左)
node = []
res = []
if not root:
return res
node.append(root)
temp = root
while node:
temp = node.pop()
if temp:
res.append(temp.val)
node.append(temp.left)
node.append(temp.right)
return res[::-1]
# 递归求解
# return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val] if root is not None else []
4.层次遍历(Level Order Traversal)-从上到下
https://leetcode.com/problems/binary-tree-level-order-traversal/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
result, level = [], [root]
while level:
result.append([node.val for node in level])
temp = []
for node in level:
temp.append(node.left)
temp.append(node.right)
level = [node for node in temp if node]
return result
5.层次遍历(Level Order Traversal)-从下到上
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def levelOrderBottom(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
result, level = [], [root]
while level:
result.insert(0, [node.val for node in level])
temp = []
for node in level:
temp.append(node.left)
temp.append(node.right)
level = [node for node in temp if node]
return result
6.之字形遍历(Zigzag Level Order Traversal)
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def zigzagLevelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
result, level, direction = [], [root], 1
while level:
result.append([node.val for node in level][::direction])
direction *= -1
temp = []
for node in level:
temp.append(node.left)
temp.append(node.right)
level = [node for node in temp if node]
return result