按升序插进去有多高

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

把 13、15、17、23、24 按升序插进一棵普通 BST。运行下面这段程序:

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

def height(node):
    if node is None:
        return 0
    return 1 + max(height(node.left), height(node.right))

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

                        
👩‍🏫
AI
💬 题目评论

全部评论