特定范围内所有子节点值的总和
我试图将其根节点的特定范围内的节点的所有子值相加。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Program {
public static class Node {
int data;
List<Node> children;
public Node(int data) {
this.data = data;
children = new ArrayList<>();
}
public void addChildren(Node... children) {
this.children.addAll(Arrays.asList(children));
}
}
public int sumRange(Node root, int min, int max) {
int sum = 0;
if(root.data >= min && root.data <= max) {
sum = root.value;
}
int size = root.children.size();
for (int i = 0; i < size; ++i) {
if(root.children.get(i).data >= min && root.children.get(i).data <= max) {
sum += sumRange(root.children.get(i), min, max);
}
}
return sum;
}
public static void main(String[] args) {
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node6 = new Node(6);
Node node7 = new Node(7);
Node node2 = new Node(2);
node2.addChildren(node3, node4);
Node node5 = new Node(5);
node5.addChildren(node6, node7);
Node node8 = new Node(8);
Node node1 = new Node(1);
node1.addChildren(node2, node5, node8);
Program program = new Program();
System.out.println(program.sumRange(node1, 3, 5)); // Should print 12
System.out.println(program.sumRange(node2, 3, 5)); // Should print 7
}
}
我尝试使用这段代码来计算特定范围之间的节点总和:
public int sumRange(Node root, int min, int max) {
int sum = 0;
if(root.data >= min && root.data <= max) {
sum = root.value;
}
int size = root.children.size();
for (int i = 0; i < size; ++i) {
if(root.children.get(i).data >= min && root.children.get(i).data <= max) {
sum += sumRange(root.children.get(i), min, max);
}
}
return sum;
}
并且,它的总和正确地只有没有子节点的节点。在这一行中,node1 有多个子节点,它应该只计算特定范围节点,其总和结果应为 12。但它打印总和结果 5。
System.out.println(program.sumRange(node1, 3, 5)); // Should print 12
我也想计算所有子节点数据。任何帮助将不胜感激!
I am trying to sum the all children values from its node between a specific range from its root node.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Program {
public static class Node {
int data;
List<Node> children;
public Node(int data) {
this.data = data;
children = new ArrayList<>();
}
public void addChildren(Node... children) {
this.children.addAll(Arrays.asList(children));
}
}
public int sumRange(Node root, int min, int max) {
int sum = 0;
if(root.data >= min && root.data <= max) {
sum = root.value;
}
int size = root.children.size();
for (int i = 0; i < size; ++i) {
if(root.children.get(i).data >= min && root.children.get(i).data <= max) {
sum += sumRange(root.children.get(i), min, max);
}
}
return sum;
}
public static void main(String[] args) {
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node6 = new Node(6);
Node node7 = new Node(7);
Node node2 = new Node(2);
node2.addChildren(node3, node4);
Node node5 = new Node(5);
node5.addChildren(node6, node7);
Node node8 = new Node(8);
Node node1 = new Node(1);
node1.addChildren(node2, node5, node8);
Program program = new Program();
System.out.println(program.sumRange(node1, 3, 5)); // Should print 12
System.out.println(program.sumRange(node2, 3, 5)); // Should print 7
}
}
I have tried this code to calculate the sum of nodes between a specific range:
public int sumRange(Node root, int min, int max) {
int sum = 0;
if(root.data >= min && root.data <= max) {
sum = root.value;
}
int size = root.children.size();
for (int i = 0; i < size; ++i) {
if(root.children.get(i).data >= min && root.children.get(i).data <= max) {
sum += sumRange(root.children.get(i), min, max);
}
}
return sum;
}
And, its summation correctly the only that nodes which have no children. In this line its the node1 have multiple child and it should calculate only the specific range nodes which should sum result 12. But its printing the sum result 5.
System.out.println(program.sumRange(node1, 3, 5)); // Should print 12
I wants to calculate all the child nodes data also. Any help will be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您只访问您范围内的子节点。这意味着如果节点 2 的值为 2,则其子节点永远不会被访问,并且您无法将值 3 和 4 相加。问题的解决方案是删除循环中的 if 条件,如下所示:
如果再次尝试,节点 1 的输出应为 12 。
The problem is that you only visit child nodes that are within your range. This means if node2 has value 2 its children are never visited and you cannot sum the values 3 and 4. The solution to your problem is removing the if condition in the loop like this:
If you try again the output should be 12 for node 1.