1.左叶子之和
404.左叶子之和
1.参数:根节点;返回值:左叶子和
2.终止条件:空节点,返回0
3.单层逻辑:判断当前节点的左节点是否为左叶子,若是则加入结果值;
对于每个节点,返回当前点的左叶子值+左子树的左叶子和+右子树的左叶子和
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if root==None: return 0
r = 0
if root.left!=None and root.left.left==None and root.left.right==None:
r = root.left.val
return r+self.sumOfLeftLeaves(root.left)+self.sumOfLeftLeaves(root.right)
2.找树左下角的值
513.找树左下角的值
方法一:层序遍历
方法二:递归,最大深度
终止条件:叶子节点,深度比之前最大深度深,更新深度和结果值(相同深度只会被第一个节点修改结果值)
单层逻辑:如果有左子树,深度加一,递归,回溯;右同理
class Solution:
# 方法一:层序遍历
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
if not root: return 0
result = 0
que = deque([root])
while que:
size = len(que)
for i in range(size):
t = que.popleft()
if i==0: result = t.val
if t.left: que.append(t.left)
if t.right: que.append(t.right)
return result
# 方法二:递归,最大深度
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
if not root: return 0
result = 0
maxdepth = -1
def traversal(root:TreeNode, depth:int):
nonlocal maxdepth, result
if root.left==None and root.right==None:
if depth > maxdepth:
maxdepth = depth
result = root.val
return
if root.left:
depth+=1
traversal(root.left, depth)
depth-=1
if root.right:
depth+=1
traversal(root.right, depth)
depth-=1
return
traversal(root, 0)
return result
3.路经总和
class Solution:
# 递归,深度遍历
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False
def traversal(root:TreeNode, sum:int):
sum += root.val
if root.left==None and root.right==None:
if sum==targetSum:
return True
return False
if root.left:
if traversal(root.left, sum): return True
if root.right:
if traversal(root.right, sum):return True
return False
if traversal(root, 0): return True
return False
路径总和II
注意变量作用域、浅拷贝与深拷贝
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root: return []
result = []
results = []
def traversal(root:TreeNode, sum:int):
result.append(root.val)
sum += root.val
if root.left==None and root.right==None:
if sum==targetSum:
results.append(result.copy())
if root.left:
traversal(root.left, sum)
result.pop()
if root.right:
traversal(root.right, sum)
result.pop()
traversal(root,0)
return results
4.从中序与后序遍历序列构造二叉树
106.从中序与后序遍历序列构造二叉树
递归
参数:中序数组,后序数组
终止条件:空节点返回None,中序数组长度为一时返回该节点
单层逻辑:构建后序数组的根节点->找根节点在中序数组中的位置->将中序数组分出左右子树->根据左右子树长度,划分后序数组的左右子树->递归左右子树
返回值:根节点
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not inorder: return None
root = TreeNode(postorder[-1])
if len(inorder)==1:
return root
temp1 = inorder.index(root.val)
inleft = inorder[:temp1]#左闭右开
inright = inorder[temp1+1:]
temp2 = len(inleft)
postleft = postorder[:temp2]#左闭右开
postright = postorder[temp2:-1]
root.left = self.buildTree(inleft, postleft)
root.right = self.buildTree(inright, postright)
return root
105.从前序与中序遍历序列构造二叉树
注意切片边值
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder: return None
root = TreeNode(preorder[0])
if len(preorder)==1: return root
temp1 = inorder.index(root.val)
inleft = inorder[:temp1]
inright = inorder[temp1+1:]
temp2 = len(inleft)
preleft = preorder[1:temp2+1]
preright = preorder[temp2+1:]
root.left = self.buildTree(preleft,inleft)
root.right = self.buildTree(preright,inright)
return root
5.最大二叉树
654.最大二叉树
参考上题
class Solution:
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
if not nums: return None
root = TreeNode(max(nums))
if len(nums)==1:
return root
temp = nums.index(root.val)
nleft = nums[:temp]
nright = nums[temp+1:]
root.left = self.constructMaximumBinaryTree(nleft)
root.right = self.constructMaximumBinaryTree(nright)
return root