英文:
print all the nodes which are multiples of root node using java
问题
这是二叉树的情况。(请参见下面的图片。)我需要打印中序遍历中的所有节点,并且只能打印根节点的倍数。
对于上述二叉树,我期望的输出将是 20 10 25 5 15 30
以下是我的代码:
class Node {
int data;
Node left, right;
Node(int value) {
data = value;
left = right = null;
}
}
class Tree {
Node root;
Tree() {
root = null;
}
// 从给定数组构建二叉树的方法(请不要编辑下面给出的代码)
public Node insertNode(int[] elementsArr, Node node, int i) {
if (i < elementsArr.length) {
node = new Node(elementsArr[i]);
node.left = insertNode(elementsArr, node.left, 2 * i + 1);
node.right = insertNode(elementsArr, node.right, 2 * i + 2);
}
return node;
}
// 打印根节点的倍数的节点方法
// 注意 - 我已经更新了printNodes方法,但仍然没有得到期望的输出
public void printNodes(Node node) {
// 在这里编写你的代码
if (node == null)
return;
if(node.data % root.data == 0) {
printNodes(node.left);
System.out.print(node.data + " ");
printNodes(node.right);
}
}
}
/*不要更改主类*/
public class PrintSpecificNodes {
public static void main(String[] x) {
Tree tree = new Tree();
Scanner sc = new Scanner(System.in);
int size;
size = sc.nextInt();
if (size <= 0) {
System.out.println("Size should be a positive integer");
} else {
int[] elementsArr = new int[size];
for (int i = 0; i < size; i++) {
elementsArr[i] = sc.nextInt();
}
tree.root = tree.insertNode(elementsArr, tree.root, 0);
tree.printNodes(tree.root);
}
}
}
英文:
Suppose this is the binary tree case. (See below image.) I need to print all the nodes on in-order traversal and it must be printing multiple of root node only.
For the binary tree, above, my desired output will be 20 10 25 5 15 30
Here is my code
class Node {
int data;
Node left, right;
Node(int value) {
data = value;
left = right = null;
}
}
class Tree {
Node root;
Tree() {
root = null;
}
// Method to construct a binary tree from the given array (Do not edit the code given below)
public Node insertNode(int[] elementsArr, Node node, int i) {
if (i < elementsArr.length) {
node = new Node(elementsArr[i]);
node.left = insertNode(elementsArr, node.left, 2 * i + 1);
node.right = insertNode(elementsArr, node.right, 2 * i + 2);
}
return node;
}
// Method to print nodes that are multiple of root node
//NOte - i have updated the printNode method but still not getting desired output
public void printNodes(Node node) {
// Write your code here
if (node == null)
return;
if(node.data% root.data==0) {
printNodes(node.left);
System.out.print(node.data+ " ");
printNodes(node.right);
}
}
}
/*Don't change main class also*/
public class PrintSpecificNodes {
public static void main(String[] x) {
Tree tree = new Tree();
Scanner sc = new Scanner(System.in);
int size;
size = sc.nextInt();
if (size <= 0) {
System.out.println("Size should be a positive integer");
} else {
int[] elementsArr = new int[size];
for (int i = 0; i < size; i++) {
elementsArr[i] = sc.nextInt();
}
tree.root = tree.insertNode(elementsArr, tree.root, 0);
tree.printNodes(tree.root);
}
}
}
What changes do I have to make in method printNodes()
to get my desired output?
答案1
得分: 1
public void printNodes(Node node){
if (node == null)
return;
if(node.data==0){
System.out.println("除以零是未定义的");
return;
}
printNodes(node.left);
if(node.data % root.data == 0){
System.out.print(node.data + " ");
}
printNodes(node.right);
}
这对我起了作用。
英文:
public void printNodes(Node node){
if (node == null)
return;
if(node.data==0){
System.out.println("Division by zero is undefined");
return;
}
printNodes(node.left);
if(node.data % root.data == 0){
System.out.print(node.data + " ");
}
printNodes(node.right);
}
this worked for me
答案2
得分: 0
System.out.print(node.data % node.data + " ");
将得到 0
方法 printNodes 需要知道根节点的值
public void printNodes(Node node) {
if (node == null) return;
printNodes(node.left);
if(node.data % root.data == 0){
System.out.println(node.data);
}
printNodes(node.right);
}
英文:
System.out.print(node.data % node.data + " ");
will get 0
method printNodes should know the value of root
public void printNodes(Node node) {
if (node == null) return;
printNodes(node.left);
if(node.data % root.data == 0){
System.out.println(node.data);
}
printNodes(node.right);
}
答案3
得分: 0
public void printNodes(Node node){
// 在这里编写你的代码
if(node == null)
return;
if(node.data == 0){
System.out.println("除以零未定义");
return;
}
printNodes(node.left);
if(node.data % root.data == 0){
System.out.print(node.data + " ");
}
printNodes(node.right);
}
我认为这应该解决了你的问题,因为这是一个中序遍历,它应该是左-根-右的顺序,并且还应该检查某个数字是否为0。如果是0,则不会打印任何内容(除以0未定义)。
英文:
public void printNodes(Node node){
// Write your code here
if(node == null)
return ;
if(node.data==0){
System.out.println("Division by zero is not defined");
return;
}
printNodes(node.left);
if(node.data%root.data==0)
{
System.out.print(node.data+ " ");
}
printNodes(node.right);
}
I think this should solve your problem, since it is an Inorder transversal it should be left-Root-right and it should also check for whether some number is 0 or not. If it's a 0 then it won't print anything (division by 0 is not defined).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论