654. 最大二叉树
思路
以下是最大二叉树的构建流程:
构造树一般采用的是前序遍历,因为先构造中间节点,然后递归构造左子树和右子树。
代码
# 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 not nums:
return None
maxVal = max(nums)
ind = nums.index(maxVal)
root = TreeNode(val = maxVal)
root.left = self.constructMaximumBinaryTree(nums[:ind])
root.right = self.constructMaximumBinaryTree(nums[ind + 1:])
return root
也可以用指针来做,这样子不需要每次递归都传递一个新的数组,直接在原数组切片就好了。会节省空间。
617.合并二叉树
思路
同时遍历两棵树,前序后序都可以。
代码
因为本质上我们是需要对二叉树重新赋值的,比如说两树的值相加,或者取两树不为none的值,所以在递归的时候,需要写root1.left = self.mergeTrees(...)
和root2.right = self.mergeTrees(...)
。它其实是和之前那些单纯遍历的题目有一些区别。
或者说构造二叉树这类的题目,在遍历的时候都可以这么写。同时注意一下return的一般都是node。
# 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:
return root2
if not root2:
return root1
root1.val += root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTrees(root1.right, root2.right)
return root1
700.二叉搜索树中的搜索
思路
可以用递归或者迭代来写。具体就是对比node的值然后选择向左或向右遍历。
代码
递归
# 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 None
if root.val < val:
return self.searchBST(root.right, val)
elif root.val > val:
return self.searchBST(root.left, val)
else:
return root
迭代
# 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]:
while root:
if root.val < val:
root = root.right
elif root.val > val:
root = root.left
else:
return root
return None
98.验证二叉搜索树
思路
不能够单纯的比较左节点小于中间节点,右节点大于中间节点就完事了。需要比较的是左子树所有节点小于中间节点,右子树所有节点大于中间节点。
使用中序遍历,输出的二叉搜索树节点的数值是有序序列。有了这个特性,验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。
代码
递归
# 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 isValidBST(self, root: Optional[TreeNode]) -> bool:
minVal = float("-inf")
def helper(root):
nonlocal minVal
if root is None:
return True
left = helper(root.left)
if root.val > minVal:
minVal = root.val
else:
return False
right = helper(root.right)
return left and right
return helper(root)
迭代
# 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 isValidBST(self, root: Optional[TreeNode]) -> bool:
stack = []
cur = root
pre = None
while cur or stack:
if cur:
stack.append(cur)
cur = cur.left
else:
cur = stack.pop()
if pre and cur.val <= pre.val:
return False
pre = cur
cur = cur.right
return True