打印所有是根节点倍数的节点,使用 Java。

huangapple go评论82阅读模式
英文:

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.) &nbsp; I need to print all the nodes on in-order traversal and it must be printing multiple of root node only.

打印所有是根节点倍数的节点,使用 Java。

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 &lt; 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+ &quot; &quot;);
printNodes(node.right);
}
}
}
/*Don&#39;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 &lt;= 0) {
System.out.println(&quot;Size should be a positive integer&quot;);
} else {
int[] elementsArr = new int[size];
for (int i = 0; i &lt; 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(&quot;Division by zero is undefined&quot;);
return;
}
printNodes(node.left);
if(node.data % root.data == 0){
System.out.print(node.data + &quot; &quot;);
}
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 + &quot; &quot;);

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(&quot;Division by zero is not defined&quot;);
return;
}
printNodes(node.left);
if(node.data%root.data==0)
{
System.out.print(node.data+ &quot; &quot;);
}
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).

huangapple
  • 本文由 发表于 2020年9月29日 16:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64116005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定