代码训练营 Day17 | 654.最大二叉树 | 617.合并二叉树 | 700.二叉搜索树中的搜索 | 98.验证二叉搜索树

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

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值