Binary Tree: summation of all nodes between a min and max value

旧巷老猫 提交于 2021-01-29 16:46:00

问题


I have an assignment where I am given the root of a randomly generated BST. I am given randomly generated test cases for this assignment.

The assignment description is the following:

You are given the root node of a binary search tree, T, and two integers: min, and max. Note that min and max are not necessarily stored in the tree. Determine the sum of all keys stored in T that are larger than or equal to min, and smaller than or equal to max. Implement your algorithm recursively

I am not allowed to have global variables or create helper functions

my current code is this:

private static int problem2(Node root, int min, int max){

 if(root = null){
    return 0;
 }

 if(root.key < min || root.key > max){     
   int lft = problem2(root.left, min, max);
   int rgt = problem2(root.right, min, max);
   int sum = lft + rgt;
   return sum;
 }

}

my issue is that if at any point during my recursion, the node trips the base case and causes my function to not complete itself properly. I believe that my ordering may be to blame.

The node classed is defined as:

private static class Node
{
    public Node left = null;
    public Node right = null;

    public int key;

    public Node(int key)
    {
        this.key = key;
    }
}

回答1:


Think about it this way: the moment you reach a node which isn't in the desired range, your algorithm stops. What happens if the root itself is out of range? You'll just return 0 right off the bat. You need to fix your exit condition so that your recursion stops only when you've reached a null, since you can't know how many nodes that are within range of [min, max] are present in the sub-tree of the node you're currently in. Then, in the recursion itself, you should make the decision whether to add the current node to the overall sum or not.

Possible implementation - spoilers ahead, I suggest you only look after you've solved it yourself:

private static int problem2(Node root, int min, int max){

 if(root == null){
    return 0;
 }

 int sum = 0;
 // No point in keeping the recursion right/left if the current key is
 // larger/smaller than the desired range - usage of BST property:
 if (root.key <= max) {
    sum += problem2(root.right, min, max);
 }
 if (root.key >= min) {
    sum += problem2(root.left, min, max);
 }
 // If root is within range add it to sum:
 if (root.key <= max && root.key >= min){
    return root.key + sum;
 } else {
    return sum;
 }

}

Explanation: each recursive call sums up the results of the left and right subtrees. The key of the node you're currently in is added to the calculation iff it is within the desired range of [min, max].




回答2:


Problem I believe lies here:

if(root == null || root.key < min || root.key > max){
    return 0;
 }

Imagine this case1 : min:10, max:20, root.key:8, here this would directly return 0, but it might be possible that there are some nodes on right child of this root that might be within the accepted range.

Similarly, case1 : min:10, max:20, root.key:22, here this would directly return 0, but it might be possible that there are some nodes on left child of this root that might be within the accepted range.

you need to modify your code in such a way that if root.key < min, then look for nodes on right child, while if root.key > max, then look for nodes on left child of the root.

Also, both the if conditions in your code are same, hence your flow would never enter the definition of the second if block.




回答3:


Since its obviously an assignment for educational purposes, I won't post a full solution, however I'll give hints that might help:

Basically you do everything right, the method definition is good.

However there are a couple of mistakes in the method itself:

First off, this piece of code:

if(root == null || root.key < min || root.key > max){
    return 0;
}

The first condition is for the cases where you've reached the end, ok. But what about the rest? Does the fact that the certain node is less than 'min.' implies that all its "children" also won't be able to "contribute" to the sum and will necessarily be "out of range"?

Disclaimer: I here rely on the fact that its a binary tree (as written in the title of the question), so I don't assume any ordering of elements. Even if its a Binary Search Tree my comment is still valid (think about why).

Now, the recursion itself:

if(root.key < min || root.key > max){     
   int lft = problem2(root.left, min, max);
   int rgt = problem2(root.right, min, max);
   int sum = lft + rgt;
   return sum;
 }

What you say here basically is "if my current root's key is out of [min,max] range then enter the recursion". Instead you should be saying "if my current root's key is within the [min,max] range then enter the recursion"

Note, that due to my first comment, this code also might change - just keep an eye on the corner cases



来源:https://stackoverflow.com/questions/62165816/binary-tree-summation-of-all-nodes-between-a-min-and-max-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!