tasks for today:
1. 654.最大二叉树
2. 617.合并二叉树
3. 700.二叉搜索树中的搜索
4. 98.验证二叉搜索树
1. 654.最大二叉树
this practice has the similar mid thread with the last practice for yesterday:
1.identify the special condition; 2.identify the mid val to constrcut the root node; 3.identify the index of mid_val in the nums, so that the essense for recursive function, which is the nums, will be construct, splitting the left_nums for the root.left and right_nums for root.right; 4. do the recusive to cinstruct the root.left and root.right;
# 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]:
# 1st step: special condition
if not nums:
return None
# 2nd identify the mid root
mid_val = max(nums)
root = TreeNode(mid_val)
# 3rd identify the segemntation point
seg_id = nums.index(mid_val)
left_num = nums[:seg_id]
right_num = nums[seg_id+1:]
# 4th step recursive
root.left = self.constructMaximumBinaryTree(left_num)
root.right = self.constructMaximumBinaryTree(right_num)
return root
note:conresponding max() for Go
index := findMax(nums)
2. 617.合并二叉树
In this practice, the special point to be noted is how to traverse two trees sumulataneously;
another point is, method1 is creating a new tree, while method2 is directly doing modification on tree1
# 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]:
# method 1
if root1 is None: return root2
if root2 is None: return root1
root = TreeNode(root1.val + root2.val)
root.left = self.mergeTrees(root1.left, root2.left)
root.right = self.mergeTrees(root1.right, root2.right)
return root
# method2
if root1 is None: return root2
if root2 is None: return root1
# 上面的递归终止条件保证了代码执行到这里root1, root2都非空.
root1.val += root2.val # 中
root1.left = self.mergeTrees(root1.left, root2.left) #左
root1.right = self.mergeTrees(root1.right, root2.right) # 右
return root1
3. 700.二叉搜索树中的搜索
BST binary search tree features:
二叉搜索树是一个有序树:
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉搜索树
This practice is vary suitable for BFS, 层序遍历
# 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 root is None or root.val == val:
return root
nodeQueue = collections.deque([root])
while nodeQueue:
cur = nodeQueue.popleft()
if cur.val == val:
return cur
if cur.left:
nodeQueue.append(cur.left)
if cur.right:
nodeQueue.append(cur.right)
return None
But it can alse be solved by DFS, which need a bit knowledge about the binary serach tree's features:
# 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 root is None or root.val == val:
return root
if root.val > val:
return self.searchBST(root.left, val)
if root.val < val:
return self.searchBST(root.right, val)
4. 98.验证二叉搜索树
this practice is suitable for DFS, inorder travese (中序遍历), not so suitable for BFS, but still can solve, but I dont like
# 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.max_val = - float('inf')
def isValidBST(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
left_bool = self.isValidBST(root.left)
if self.max_val < root.val: self.max_val = root.val
else: return False
right_bool = self.isValidBST(root.right)
return left_bool and right_bool