简体   繁体   English

使用java打印所有根节点的倍数的节点

[英]print all the nodes which are multiples of root node using java

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对于上面的二叉树,我想要的输出是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?我必须在方法printNodes()进行哪些更改才能获得所需的输出?

System.out.print(node.data % node.data + " ");

will get 0将得到 0

method printNodes should know the value of root方法 printNodes 应该知道 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);
    }
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.我认为这应该可以解决您的问题,因为它是一个中序横向,它应该是 left-Root-right 并且它还应该检查某个数字是否为 0。 If it's a 0 then it won't print anything (division by 0 is not defined).如果它是一个 0 那么它不会打印任何东西(除以 0 没有定义)。

public void printNodes(Node node){ public void printNodes(节点节点){

    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这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打印所有节点,包括节点 java - Print all nodes including nodes java Java:创建方法以打印1到n之间的3的所有倍数吗? - Java: Create method to print all multiples of 3 between 1 and n? 在二叉树中为给定的输入节点打印祖先节点,而无需在Java中使用递归 - print ancestor nodes in a binary tree for given input node without using recursion in java 获取节点 XML Java 的所有子节点 - Get all child nodes of a node XML Java Java从根目录搜索具有相同标签的节点的所有路径 - Java Search all paths of nodes with same label from root 如何打印大于树中特定节点的所有节点? - How can print all nodes that are greater than specific node in tree? 打印没有兄弟的二叉树中的所有节点? - Print all the nodes in a binary tree which do not have sibling? 无法使用Java从根目录创建Zookeeper节点(尚不存在) - Could not create zookeeper node (which does not already exist) from the root using java Java中的3和5的倍数,5的倍数和3的倍数 - Multiples of 3 and 5, multiples of 5 and multiples of 3 in Java 用java编写一个程序来打印从50到250(包括50和250)的所有3的倍数且不能被9整除的数字之和 - Write a program in java to print the sum of all numbers from 50 to 250(inclusive of 50 and 250) that are multiples of 3 and not divisible by 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM