101、对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
解法一:按照进阶的提示用递归的办法来解决问题。
一个树是否对称:
如果只有root,或没有root,则肯定对称。如果有左右子树,则两者要对称,这意味着左子树的左子树与右子树的右子树对称,左子树的右子树与右子树的左子树相对应,这样就可以进行递归了
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root:
return True
def isSym(left,right):
if left==None and right==None:
return True
elif left==None or right==None:
return False
elif left.val!=right.val:#比较一定要用.val
return False
else:
return isSym(left.left,right.right) and isSym(left.right,right.left)
return isSym(root.left,root.right)
执行用时:40 ms, 在所有 Python3 提交中击败了90.21%的用户
内存消耗:13.9 MB, 在所有 Python3 提交中击败了28.95%的用户
解法二:迭代法和递归法有些类似,改动是把需要比较的对象成对的放到了一个列表里。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root:
return True
pair=[root.left,root.right]
while pair:
right=pair.pop()
left=pair.pop()
if left==None and right==None:
continue#要用continue,因为这不是按顺序pop出来的
elif left==None or right==None:
return False
elif left.val!=right.val:#比较一定要用.val
return False
else:
pair.extend([left.left,right.right,left.right,right.left])
return True
执行用时:48 ms, 在所有 Python3 提交中击败了50.94%的用户
内存消耗:13.9 MB, 在所有 Python3 提交中击败了25.59%的用户
这个方法比较慢就是因为它不是按顺序一层一层的判断
102、二叉树的层序遍历
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
解:标准的bfs
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
gray=[]
ans=[]
if not root:
return []
ans.append([root.val])
if root.left:
gray.append(root.left)
if root.right:
gray.append(root.right)
if not gray:
return ans
last=gray[-1]#记录一层的最后一个元素
curList=[]
while gray:
a=gray.pop(0)
if not a:
continue
curList.append(a.val)
if a.left:
gray.append(a.left)
if a.right:
gray.append(a.right)
if a==last:
ans.append(curList)
curList=[]
if not gray:
break
last=gray[-1]
return ans
执行用时:40 ms, 在所有 Python3 提交中击败了82.85%的用户
内存消耗:14 MB, 在所有 Python3 提交中击败了62.19%的用户
103、二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
解:与上一题没有太大差别,加个层数判断奇偶决定curList是正序加入还是倒叙加入就行了。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
gray=[]
ans=[]
if not root:
return []
ans.append([root.val])
if root.left:
gray.append(root.left)
if root.right:
gray.append(root.right)
if not gray:
return ans
last=gray[-1]#记录一层的最后一个元素
curList=[]
depth=2
while gray:
a=gray.pop(0)
if not a:
continue
curList.append(a.val)
if a.left:
gray.append(a.left)
if a.right:
gray.append(a.right)
if a==last:
if depth==2:
ans.append(curList[::-1])
depth=1
else:
ans.append(curList)
depth=2
curList=[]
if not gray:
break
last=gray[-1]
return ans
执行用时:36 ms, 在所有 Python3 提交中击败了92.71%的用户
内存消耗:14.1 MB, 在所有 Python3 提交中击败了6.39%的用户
104、二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
解:跟上两道题是一个思路
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxDepth(self, root: TreeNode) -> int:
gray=[]
if not root:
return 0
if root.left:
gray.append(root.left)
if root.right:
gray.append(root.right)
if not gray:
return 1
depth=2
last=gray[-1]#记录一层的最后一个元素
while gray:
a=gray.pop(0)
if not a:
continue
if a.left:
gray.append(a.left)
if a.right:
gray.append(a.right)
if a==last:
if not gray:
return depth
depth=depth+1
last=gray[-1]
return depth
执行用时:44 ms, 在所有 Python3 提交中击败了97.09%的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了87.91%的用户
105、从前序与中序遍历序列构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
解:前序遍历的数组结构是根节点,左子树,右子树,
后序遍历的结构是左子树,根节点,右子树。所以可以通过两者找到左子树集合,右子树集合和根节点,这样就可以递归了。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
if not inorder:
return None
root=TreeNode(preorder[0])
i=inorder.index(root.val)
root.left=self.buildTree(preorder[1:i+1], inorder[:i])
root.right=self.buildTree(preorder[i+1:], inorder[i+1:])
return root
执行用时:180 ms, 在所有 Python3 提交中击败了66.00%的用户
内存消耗:87.4 MB, 在所有 Python3 提交中击败了60.77%的用户