654.最大二叉树
题目链接:LeetCode - The World's Leading Online Programming Learning Platform
题目链接/文章讲解:代码随想录
视频讲解:又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树_哔哩哔哩_bilibili
解题思路:
前序遍历
设置basic condition 如果nums为空的话 直接返回
进入递归 找到最大的值并设置成当前root 以其index左右分成两个part 先向左递归再向右递归 返回上来的的root值做左右子树的值
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def constructMaximumBinaryTree(self, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
if not nums:
return
max_val=max(nums)
max_index=nums.index(max_val)
root=TreeNode(max_val)
left_part=nums[:max_index]
right_part=nums[max_index+1:]
root.left=self.constructMaximumBinaryTree(left_part)
root.right=self.constructMaximumBinaryTree(right_part)
return root
617.合并二叉树
题目链接:LeetCode - The World's Leading Online Programming Learning Platform
题目链接/文章讲解:代码随想录
视频讲解:一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树_哔哩哔哩_bilibili
解题思路:
先判断第一个树是不是为空 为空的话返回另一个树 然后判断第二个树是不是空 如果是空的话 直接返回最开始的树。反之 如果两个树此时的节点都不为空的话 进入前序遍历递归 将此时第二个节点的值加到第一个节点 接着进行左递归和右递归。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def mergeTrees(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: 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.二叉搜索树中的搜索
题目链接:LeetCode - The World's Leading Online Programming Learning Platform
题目链接/文章讲解: 代码随想录
视频讲解:不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索_哔哩哔哩_bilibili
解题思路:
basic condition 如果node为空直接返回空 如果node的值等于target直接返回当前node
进入左递归如果node当前值大于target 反之进去右递归。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if not root:
return None
if root.val==val:
return root
elif root.val>val:
return self.searchBST(root.left,val)
elif root.val<val:
return self.searchBST(root.right,val)
98.验证二叉搜索树
题目链接:LeetCode - The World's Leading Online Programming Learning Platform
题目链接/文章讲解:代码随想录
视频讲解:你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树_哔哩哔哩_bilibili
解题思路:
搜索树的中序遍历是increasing order
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
res=[]
def traversal(root):
if not root:
return
traversal(root.left)
res.append(root.val)
traversal(root.right)
def is_increasing(ls):
for i in range(len(ls)-1):
if ls[i]>=ls[i+1]:
return False
return True
traversal(root)
return is_increasing(res)