654.最大二叉树
1. 找到最大的元素,就是根元素
2. 从最大的元素切开,左边是左区间,
1. 从左区间找最大的数,作为左子树的父节点
2. 右区间同理
3. 使用前序遍历; 凡是构造二叉树类的题目都是要用前序遍历
# 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
"""
# recursion stop condition
if len(nums) == 1:
# mean's there only has root, also it's leaf node return that value
return TreeNode(nums[0])
# middle traversal process
maxValue = 0
index = 0
for i in range(len(nums)):
# find the largest number in the array
if(nums[i] > maxValue):
# store maxValue
maxValue = nums[i]
# store maxValue index
index = i
# construct root node
node = TreeNode(maxValue)
# left traversal process
if(index > 0): # makesure there is value inside nums array
# define left interval [0,index)
leftInterval = nums[0:index]
node.left = self.constructMaximumBinaryTree(leftInterval )
# right traversal process
if(index < len(nums)-1):
rightInterval = nums[index+1:]
node.right = self.constructMaximumBinaryTree(rightInterval)
# return the root
return node
617.合并二叉树
1. 递归终止条件:
1. 当root1为none时时返回root2
2. 当root2为none时返回root1
2. 处理递归单层条件: 可以使用 前中后序任何一种
1. 处理中序:
把其中一棵树当作主树(这里用root1),加等另外一棵树
2. 处理左边和右边的递归调教
1. 左边的递归传参 root1和root2的左边 root1.left root2.left
2. 右边的递归传参 root1和root2的右边 root1.right root2.right
3. 返回root1 因为root1是我们主树
# 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
"""
# let recursion fucntion have two arguments
# one is for tree1
# second is for tree2
# stop condition
# 1. root1 is empty, we return root2
if root1 == None:
return root2
# 2. root2 is empty, we return root1
if root2 == None:
return root1
# let tree2 value add to tree1
root1.val += root2.val # middle
# use pre order traversal
root1.left = self.mergeTrees(root1.left,root2.left) # left
root1.right = self.mergeTrees(root1.right,root2.right) # right
return root1
700.二叉搜索树中的搜索
1. 二叉搜索树种,根节点的数值要比左子树里边的所有数值要大,根节点的数值要比右子树的所有数值要小
2. 同时左右子树也符合上面的规则
3. 这样就是一颗二叉搜索树
# 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
"""
# Iterative traversal
while(root != None):
# traversal the binary search tree
# if the root.val < val
if(val < root.val):
# we're going left sub tree
root = root.left
elif(val > root.val):
# we're going right sub tree
root = root.right
else:
# root.val == val
return root
# if can't find any return None
return None
98.验证二叉搜索树
0. 如果root为空也是二叉搜索树
1. 递归法
2. 根据二叉树特性去写这一题很简单,但注意要用中序遍历
3. 在对中节点遍历处理条件的时候可以用一个全集变量pre去保存前一个节点值
# 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 __init__(self):
self.pre = None
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
# if the root is empty
if(root == None):
return True
# left sub tree recursion
left = self.isValidBST(root.left)
# middle traversal
if(self.pre != None and self.pre.val >= root.val):
# is not valid binary search tree
return False
self.pre = root # 记录前一个节点
# right sub tree recursion
right = self.isValidBST(root.right)
return left and right