9  20
    /  \
   15   7

Depth: 3
"""


from BST import BST, Node


class Solution:
    def maxDepth(self, root: Node) -> int:
        if root is None:
            return 0

        left_depth = self.maxDepth(root.left)
        right_depth = self.maxDepth(root.right)

        return max(left_depth, right_depth) + 1  # +1 for the edge from the root connecting left and right subtree


bst = BST()
bst.insert_node(5)
bst.insert_node(1)
bst.insert_node(7)
bst.insert_node(6)
bst.insert_node(8)

print(Solution().maxDepth(bst.root))
Пример #2
0
        self._inorder(root, L, R)
        return self.sum_range

    def _inorder(self, root, L, R):

        if root is None:
            return

        if L <= root.data <= R:
            self.sum_range += root.data

        if L < root.data:
            self._inorder(root.left, L, R)

        if root.data < R:
            self._inorder(root.right, L, R)


bst = BST()
bst.insert_node(10)
bst.insert_node(5)
bst.insert_node(3)
bst.insert_node(7)
bst.insert_node(15)
bst.insert_node(18)
bst.insert_node(1)
bst.insert_node(6)

# print(Solution().rangeSumBST(bst.root, 7, 15))
print(Solution().rangeSumBST(bst.root, 6, 10))