一、今天的主要任务是学会用递归的方法去思考题目并解决题目,所以今天的题目都用递归法来解决:
1.leetcode题目 257 二叉树的所有路径(easy):
题目描述:257. 二叉树的所有路径 - 力扣(LeetCode)
解决:
递归法:
# 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 binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
result = []
if not root:
return []
def dfs(root,path): #确定参数
if root:
path += str(root.val)
if not root.left and not root.right: #确定递归的终止条件
result.append(path)
path += '->'
dfs(root.left,path)
dfs(root.right,path)
dfs(root,'')
return result
2.letcode题目 101 对称二叉树(easy):
题目描述:101. 对称二叉树 - 力扣(LeetCode)
解决:
递归法:
# 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 isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def dfs(left,right):
if not left and right:return False
elif left and not right:return False
elif not left and not right:return True
elif left.val!=right.val:return False
return dfs(left.left,right.right) and dfs(left.right,right.left)
return dfs(root.left,root.right)
3.leetcode题目 翻转二叉树(easy):
题目描述:226. 翻转二叉树 - 力扣(LeetCode)
解决:
递归法:
# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
def dfs(root):
if root:
root.left,root.right = root.right,root.left
dfs(root.left)
dfs(root.right)
dfs(root)
return root
4.leetcode题目 106 从中序与后续序列构造二叉树(medium):
题目描述:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
思路:
解决:
# 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 buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not postorder:return
root_val = postorder[-1]
root = TreeNode(root_val)
split_i = inorder.index(root_val)
inorder_left = inorder[:split_i]
inorder_right = inorder[split_i+1:]
postorder_left = postorder[:len(inorder_left)]
postorder_right = postorder[len(inorder_left):len(postorder) -1] ##此处有坑
root.left = self.buildTree(inorder_left,postorder_left)
root.right = self.buildTree(inorder_right,postorder_right)
return root
5.leetcode题目 105 从中序与前序序列构造二叉树(medium):
题目描述:105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)
解决:
递归法
# 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 buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder:
return
root = TreeNode(preorder[0])
if not root:
return
root_val = root.val
split_idx = inorder.index(root_val)
inorder_left = inorder[:split_idx]
inorder_right = inorder[split_idx + 1:]
preorder_left = preorder[1:len(inorder_left)+1]
preorder_right = preorder[len(inorder_left)+1:]
root.left = self.buildTree(preorder_left,inorder_left)
root.right = self.buildTree(preorder_right,inorder_right)
return root
6.leetcode题目 654 最大二叉树(medium):
题目描述:654. 最大二叉树 - 力扣(LeetCode)
解决:
递归
# 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 constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
if len(nums) == 0:
return
N = max(nums)
idx = nums.index(N)
root = TreeNode(N)
nums_left = nums[:idx]
nums_right = nums[idx+1:]
root.left = self.constructMaximumBinaryTree(nums_left)
root.right = self.constructMaximumBinaryTree(nums_right)
return root
7.leetcode题目 617 合并二叉树(easy):
题目描述:617. 合并二叉树 - 力扣(LeetCode)
解决:
递归法:
# 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 mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
if not root1 and not root2:return
elif not root1 and root2:root = root2
elif root1 and not root2:root = root1
elif root1 and not root2:root = root1
else:
root = TreeNode(root1.val + root2.val)
root.left = self.mergeTrees(root1.left,root2.left)
root.right = self.mergeTrees(root1.right,root2.right)
return root
8.leetcode题目 700 二叉搜索树中的搜索(easy):
此题的先验知识需知道:
二叉搜索树满足如下性质:
- 左子树所有节点的元素值均小于根的元素值;
- 右子树所有节点的元素值均大于根的元素值。
题目描述:700. 二叉搜索树中的搜索 - 力扣(LeetCode)
解决:
递归法:
# 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 searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
if not root:
return
if root.val == val:
return root
return self.searchBST(root.left if val<root.val else root.right,val)
9.leetcode题目 98 验证二叉搜索树(medium):
解决本题先验知识:二叉搜索树的中序遍历是升序序列
题目描述:98. 验证二叉搜索树 - 力扣(LeetCode)
解决:
递归法:
# 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 __init__(self):
self.maxVal = float('-inf') # 因为后台测试数据中有int最小值
def isValidBST(self, root):
if root is None:
return True
left = self.isValidBST(root.left)
# 中序遍历,验证遍历的元素是不是从小到大
if self.maxVal < root.val:
self.maxVal = root.val
else:
return False
right = self.isValidBST(root.right)
return left and right