[英]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.