1. 判断一个树是否是BST
# Python program to check if a binary tree is bst or not

INT_MAX = 4294967296
INT_MIN = -4294967296

# A binary tree node
class Node:

    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


# Returns true if the given tree is a binary search tree
# (efficient version)
def isBST(node):
    return (isBSTUtil(node, INT_MIN, INT_MAX))

# Retusn true if the given tree is a BST and its values
# >= min and <= max
def isBSTUtil(node, mini, maxi):
    
    # An empty tree is BST
    if node is None:
        return True

    # False if this node violates min/max constraint
    if node.data < mini or node.data > maxi:
        return False

    # Otherwise check the subtrees recursively
    # tightening the min or max constraint
    return (isBSTUtil(node.left, mini, node.data -1) and
        isBSTUtil(node.right, node.data+1, maxi))

# Driver program to test above function
root = Node(4)
root.left = Node(2)
root.right = Node(5)
root.left.left = Node(1)
root.left.right = Node(3)

if (isBST(root)):
    print ("Is BST")
else:
    print ("Not a BST")

  1. 求一棵平衡二叉树的最小深度
# Python program to find minimum depth of a given Binary Tree

# Tree node
class Node:
    def __init__(self , key):
        self.data = key
        self.left = None
        self.right = None

def minDepth(root):
    # Corner Case.Should never be hit unless the code is
    # called on root = NULL
    if root is None:
        return 0
    
    # Base Case : Leaf node.This accounts for height = 1
    if root.left is None and root.right is None:
        return 1
    
    # If left subtree is Null, recur for right subtree
    if root.left is None:
        return minDepth(root.right)+1
    
    # If right subtree is Null , recur for left subtree
    if root.right is None:
        return minDepth(root.left) +1
    
    return min(minDepth(root.left), minDepth(root.right))+1

# Driver Program
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print (minDepth(root))

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)  
  1. 判断一棵二叉树是否高度平衡
# A class to store a binary tree node
class Node:
    def __init__(self, key, left=None, right=None):
        self.key = key
        self.left = left
        self.right = right
 
 
# Recursive function to check if a given binary tree is height-balanced or not
def isHeightBalanced(root, isBalanced=True):
 
    # base case: tree is empty or not balanced
    if root is None or not isBalanced:
        return 0, isBalanced
 
    # get the height of the left subtree
    left_height, isBalanced = isHeightBalanced(root.left, isBalanced)
 
    # get the height of the right subtree
    right_height, isBalanced = isHeightBalanced(root.right, isBalanced)
 
    # tree is unbalanced if the absolute difference between the height of
    # its left and right subtree is more than 1
    if abs(left_height - right_height) > 1:
        isBalanced = False
 
    # return height of subtree rooted at the current node
    return max(left_height, right_height) + 1, isBalanced
 
 
if __name__ == '__main__':
 
    ''' Construct the following tree
              1
            /   \
           /     \
          2       3
         / \     /
        4   5   6
    '''
 
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right.left = Node(6)
 
    if isHeightBalanced(root)[1]:
        print('Binary tree is balanced')
    else:
        print('Binary tree is not balanced')

你可能感兴趣的:(树)