修剪后如何检查树值?

时间:2018-09-11 06:33:38

标签: python printing binary-search-tree

我修剪了一个BST,但是之后如何检查树的值?

def TrimTree(tree, minVal, maxVal):
    if not tree:
        return 
    tree.left=TrimTree(tree.left, minVal, maxVal)
    tree.right=TrimTree(tree.right, minVal, maxVal)
    if minVal< tree< maxVal:
        return tree
    if tree.val < minVal:
        return tree.right 
    if tree.val>maxVal:
        return  tree.left 

我的树定义如下,我也尝试打印根值,但是它给我一个错误,说“ NoneType”对象没有属性“ val”

class Node:
    def __init__(self, val):
        self.val= val
        self.right =None
        self.left= None
root =Node(10)
root.right =Node(35)
root.left=Node(5)
tree1=TrimTree(root, 8, 25)
print tree1.val

如何打印值并检查修整是否正常? 谢谢!

0 个答案:

没有答案