二叉树leetcode(98,102,107)python

二叉树具体知识 数据结构(十四)——二叉树

leetcode98. Validate Binary Search Tree

题目
Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
Example 1:

Input:
    2
   / \
  1   3
Output: true
Example 2:

    5
   / \
  1   4
     / \
    3   6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
             is 5 but its right child's value is 4.
思路

递归
1.空的返回true
2.左子树不空时,根结点要比所有左子树上的结点大,根结点大于左子树的最大值—左子树的右下角的结点。右子树不空时,根节点小于右子树的最小值小—右子树左下角的结点。

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        return self.is_valid(root,None,None)
    
    def is_valid(self,root,min,max):
        if not root:
            return True
        if (min is not None and root.val <=min) or (max is not None and root.val >= max):
            return False
        
        return self.is_valid(root.left, min,root.val) and self.is_valid(root.right, root.val,max)

leetcode102. Binary Tree Level Order Traversal

题目

Given a binary tree, return the level order traversal of its nodes' values.
 (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
return its level order traversal as:
[
  [3],
  [9,20],
  [15,7]
]

思路

递归
目前答案列表答案个数等于层数时,当前层数的列表append新元素
ans[level].append(root.val) 保证正确层数

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        ans = []
        def order(root,level):
            if root != None:
                if len(ans) == level:
                    ans.append([])
                ans[level].append(root.val)
                order(root.left,level+1)
                order(root.right,level+1)
        order(root,0)
        return ans   

leetcode107. Binary Tree Level Order Traversal II

思路

在上面 leetcode102遍历基础上颠倒答案ans.reverse()

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        ans = []
        def order(root,level):
            if root != None:
                if len(ans) == level:
                    ans.append([])
                ans[level].append(root.val)
                order(root.left,level+1)
                order(root.right,level+1)
        order(root,0)
        ans.reverse()
        return ans  

知识回顾

回顾

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值