换个顺序插入之后有多高

👁️ 0 人浏览 💬 0 人评论 ❤️ 添加收藏

同样五个数,改成 17、24、15、13、23 的顺序插入:

class BNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def insert(root, val):
    if root is None:
        return BNode(val)
    if val < root.val:
        root.left = insert(root.left, val)
    elif val > root.val:
        root.right = insert(root.right, val)
    return root

def height(node):
    if node is None:
        return 0
    a = height(node.left)
    b = height(node.right)
    return 1 + (a if a > b else b)

root = None
for v in [17, 24, 15, 13, 23]:
    root = insert(root, v)
print(height(root))
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论