证明某二叉树为二叉搜索树(Python实现)

!/usr/bin/env python3                                                           
                                                                                
lass TreeNode:                                                                  
   """                                                                          
   二叉树基类, 双向链表, 用于存储二叉树数据                                     
   """                                                                          
                                                                                
   def __init__(self, val, left_node=None, right_node=None):                    
       #  父节点值                                                              
        self.val = val                                                          
        #  左子树                                                               
        self.left = left_node                                                   
        #  右子树                                                               
        self.right = right_node                                                 
        #  爷节点作为最大值                                                     
        self.grandpa_l = self.get_max() + 1                                     
        #  爷节点作为最小值                                                     
        self.grandpa_r = self.get_min()                                         
                                                                                
    def get_max(self):                                                          
        max_node = self.right or self                                           
        return max_node.val                                                     
                                                                                
    def get_min(self):                                                          
        min_node = self.left or self                                            
        return min_node.val                                                     
                                                                                
    def is_sorted(self):                                                        
        if self.left and self.left.val >= self.val:                             
            return False                                                        
                                                                                
        if self.right and self.val > self.right.val:                            
            return False                                                        
                                                                                
        if self.get_max() >= self.grandpa_l:                                    
            return False                                                        
                                                                                
        if self.get_min() < self.grandpa_r:                                     
            return False                                                        
                                                                                
        return True                                                             
                                                                                
                                                                                
def is_search_tree(treeNode):                                                   
    if not treeNode.is_sorted():                                                
        return False                                                            
                                                                                
    left_flag = right_flag = True                                               
                                                                                
    if treeNode.left:                                                           
        treeNode.left.grandpa_l = treeNode.val                                  
        left_flag = is_search_tree(treeNode.left)                               
                                                                                
    if treeNode.right:                                                          
        treeNode.right.grandpa_r = treeNode.val                                 
        right_flag = is_search_tree(treeNode.right)                             
                                                                                
    return left_flag and right_flag                                             
                                                                                
                                                                                
#  测试代码                                                                     
if __name__ == '__main__':                                                      
    left1 = TreeNode(1)                                                         
    right1 = TreeNode(3)                                                        
    tree1 = TreeNode(2, left1, right1)                                          
    left2 = TreeNode(5)                                                         
    right2 = TreeNode(7)                                                        
    tree2 = TreeNode(6, left2, right2)                                          
    tree = TreeNode(4, tree1, tree2)                                            
    print(is_search_tree(tree1))                                                
    print(is_search_tree(tree2))                                                
    print(is_search_tree(tree))

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值