69. 二叉树的层次遍历
给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)
样例
给一棵二叉树
{3,9,20,#,#,15,7}
:3 / \ 9 20 / \ 15 7
返回他的分层遍历结果:
[[3],[9,20],[15,7]]
挑战
挑战1:只使用一个队列去实现它
挑战2:用BFS算法来做
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: A Tree
@return: Level order a list of lists of integer
"""
def levelOrder(self, root):
# write your code here
if not root:
return []
queue = []
queue.append(root)
result=[]
while len(queue) > 0:
item=[]
for _ in range(len(queue)):
tmp = queue.pop(0)
item.append(tmp.val)
if tmp.left:
queue.append(tmp.left)
if tmp.right:
queue.append(tmp.right)
result.append(item)
return result
70. 二叉树的层次遍历 II
给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)
样例
给出一棵二叉树
{3,9,20,#,#,15,7}
,3 / \ 9 20 / \ 15 7
按照从下往上的层次遍历为:
[[15,7],[9,20],[3]]
71. 二叉树的锯齿形层次遍历
给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行)
样例
给出一棵二叉树
{3,9,20,#,#,15,7}
,3 / \ 9 20 / \ 15 7
返回其锯齿形的层次遍历为:
[[3],[20,9],[15,7]]
输入测试数据 (每行一个参数)如何创建测试数据?
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param root: A Tree
@return: A list of lists of integer include the zigzag level order traversal of its nodes' values.
"""
def zigzagLevelOrder(self, root):
# write your code here
if not root:
return []
queue = []
queue.append(root)
result=[]
biz=1
while len(queue) > 0:
item=[]
for _ in range(len(queue)):
tmp = queue.pop(0)
item.append(tmp.val)
if tmp.left:
queue.append(tmp.left)
if tmp.right:
queue.append(tmp.right)
if biz%2==1:
result.append(item)
if biz%2==0:
item.reverse()
result.append(item)
biz+=1
return result
72. 中序遍历和后序遍历树构造二叉树
根据中序遍历和后序遍历树构造二叉树
样例
给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2]
返回如下的树:
2
/ \
1 3
注意事项
你可以假设树中不存在相同数值的节点
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param inorder: A list of integers that inorder traversal of a tree
@param postorder: A list of integers that postorder traversal of a tree
@return: Root of a tree
"""
def buildTree(self, inorder, postorder):
# write your code here
if len(inorder) == 0:
return None
root=TreeNode(postorder[len(postorder)-1])
i=inorder.index(postorder[len(postorder)-1])
root.left=self.buildTree(inorder[:i],postorder[:i])
root.right=self.buildTree(inorder[i+1:],postorder[i:-1])
return root
73. 前序遍历和中序遍历树构造二叉树
根据前序遍历和中序遍历树构造二叉树.
样例
给出中序遍历:
[1,2,3]
和前序遍历:[2,1,3]
. 返回如下的树:2 / \ 1 3
注意事项
你可以假设树中不存在相同数值的节点
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param inorder: A list of integers that inorder traversal of a tree
@param postorder: A list of integers that postorder traversal of a tree
@return: Root of a tree
"""
def buildTree(self, preorder, inorder):
# write your code here
if len(preorder) == 0:
return None
# 前序遍历的第一个结点一定是根结点
root=TreeNode(preorder[0])
i = inorder.index(preorder[0])
# 递归构造左子树和右子树
root.left = self.buildTree(preorder[1: 1 + i], inorder[:i])
root.right = self.buildTree(preorder[1 + i:], inorder[i + 1:])
return root