简体   繁体   中英

Function finding the deepest sum of a binary search tree

I'm trying to make a function in python were I don't want to change the BST class at all to do this. The function is to find the sum of the path of the root to the node with the highest depth. If there is multiple nodes that have the same depth I'm looking for the maximum sum of that and return it.

What I got so far (Using a basic BST Class)

class BTNode(object):

    def __init__(self, data, left=None, right=None):

        self.data = data
        self.left = left
        self.right = right

After trying to make an algorithm to solve this for a while, I came up with a couple above but they were fails and I didn't know how to code it.

ALGORITHM:

I think this would work:

We start from the root. (The base case I think should be whenever we hit a leaf as in when there is no child in a node so no left or right child, and also when there is a left no right, or when there is a right no left)

I check the left subtree first and get the depth of it, we'll call it depth_L as well with the sum. Then I check the right subtree and we will call it depth_R then get the depth of it and its sum.

The second condition would be to check if they are equal, if they are equal then its easy and we just take the max sum of either two depths. Else we would see who has the highest depth and try to get the sum of it.

Now were I don't know how to do is a couple things.

1: I never learned optional parameters so I'm trying to avoid that while trying this exercise but I don't think I can and I'd really appreciate someone could show me some cool helper functions instead.

2: Its not the total sum of the right side or the left side its the path that I need. Its kind of confusing to think of a way to get just the path

(Heres my renewed attempt using the algorithm above):

    def deepest_sum(self, bsum = 0, depth = 0):

        # The root is in every path so
        bsum = bsum + self.data
        depth = depth + 1
        # Base case whenever we find a leaf
        if self.left == None and self.right == None:
            result = bsum,depth

        elif self.left is not None and self.right is None:
            pass

        elif self.left is None and self.right is not None:
            pass

        else:

            # Getting the left and right subtree's depth as well as their
            # sums, but once we hit a leaf it will stop 
            # These optional parameters is messing me up
            if self.left:
                (sums1, depth_L) = self.left.deepest_sum(bsum,depth)
            if self.right:
                (sums2, depth_R) = self.right.deepest_sum(bsum,depth)

            # The parameter to check if they are equal, the highest goes through
            if depth_L == depth_R:
                result = max(sums1, sums2), depth_R
            else:
                if depth_L > depth_R:
                    result = sums1, depth_L
                else:
                    result = sums2, depth_R

        return result

Stuck on the parts i mentioned. Heres an example:

>>> BST(8, BST(7, BST(10), BST(11)), BST(6, BST(11), BST(9, None, BST(14)))

37  (depth 3 is the highest so 8 + 6 + 9 + 14 is the path) 

Sorry i put BST i just forgot, its a binary Tree not BST.

I know mine gives a tuple but I can always make a helper function to fix that, I just thought it'd be easier keeping track of the nodes.

You could simplify the implementation quite a bit if the function doesn't need to be a method of BTNode . Then you could keep track of the depth & sum, iterate past the leaf and return the current depth and sum. Additionally if you return (depth, sum) tuples you can compare them directly against each other with max :

class BTNode(object):
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right

def deepest_sum(node, depth=0, current=0):
    # Base case
    if not node:
        return (depth, current)

    depth += 1
    current += node.data

    return max(deepest_sum(node.left, depth, current), 
               deepest_sum(node.right, depth, current))

tree = BTNode(8, BTNode(7, BTNode(10), BTNode(11)), BTNode(6, BTNode(11), BTNode(9, None, BTNode(14))))
print(deepest_sum(tree))

Output:

(4, 37)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM