In this problem, if I call build function… I need to use static but when I make the function static, I cannot create new node
Problem with creating node
@Shlok
Make the following changes to your code
public static class Node {
int data;
Node left;
Node right;
Node() {
}
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public static Node build(int[] p, int[] i, int s, int e) {
if (s > e) {
return null;
}
int num = p[k++];
int j = 0;
for (j = s; j <= e; j++) {
if (num == i[j])
break;
}
Node root = new Node(num);
root.left = build(p, i, s, j - 1);
root.right = build(p, i, j + 1, e);
return root;
}
now im getting an error on Node root = new Node(num);
Error : No enclosing instance of type NodesAtDistanceK is accessible. Must qualify the allocation with an enclosing instance of type NodesAtDistanceK (e.g. x.new A() where x is an instance of NodesAtDistanceK).
@Shlok here is complete code
import java.util.*;
public class Main {
static int k = 0;
private static class Node {
int data;
Node left;
Node right;
Node() {
}
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public static Node build(int[] p, int[] i, int s, int e) {
if (s > e) {
return null;
}
int num = p[k++];
int j = 0;
for (j = s; j <= e; j++) {
if (num == i[j])
break;
}
Node root = new Node(num);
root.left = build(p, i, s, j - 1);
root.right = build(p, i, j + 1, e);
return root;
}
public void find(Node node, int k) {
if (node == null) {
return;
}
if (k == 0) {
System.out.print(node.data + " ");
return;
}
find(node.left, k - 1);
find(node.right, k - 1);
}
void preOrder(Node node) {
if (node == null)
return;
System.out.println(node.data);
preOrder(node.left);
preOrder(node.right);
}
public int distanceK(Node node, int k, int inf) {
if (node == null) {
return -1;
}
if (node.data == inf) {
find(node.left, k - 1);
find(node.right, k - 1);
return 0;
}
int l = distanceK(node.left, k, inf);
if (l != -1) {
if (l + 1 == k) {
System.out.print(node.data + " ");
} else {
find(node.right, k - l - 2);
}
return l + 1;
}
int r = distanceK(node.right, k, inf);
if (r != -1) {
if (r + 1 == k) {
System.out.print(node.data + " ");
} else {
find(node.left, k - r - 2);
}
return r + 1;
}
return -1;
}
public static void printkdistancedown(Node node, int k) {
if (node == null || k < 0) {
return;
}
if (k == 0) {
System.out.println(node.data + " ");
return;
}
printkdistancedown(node.left, k - 1);
printkdistancedown(node.right, k - 1);
}
public static int printkdistance(Node node, int target, int k) {
if (node == null)
return -1;
if (node.data == target) {
printkdistancedown(node, k);
return 0;
}
int dl = printkdistance(node.left, target, k);
if (dl != -1) {
if (dl + 1 == k) {
System.out.print(node.data + " ");
} else
printkdistancedown(node.right, k - dl - 2);
return 1 + dl;
}
int dr = printkdistance(node.right, target, k);
if (dr != -1) {
if (dr + 1 == k)
System.out.print(node.data + " ");
else
printkdistancedown(node.left, k - dr - 2);
return 1 + dr;
}
return -1;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] preorder = new int[n];
int[] inorder = new int[n];
for (int i = 0; i < n; i++) {
preorder[i] = scn.nextInt();
}
for (int i = 0; i < n; i++) {
inorder[i] = scn.nextInt();
}
Node nn = build(preorder, inorder, 0, n - 1);
int t = scn.nextInt();
while (t > 0) {
int inf = scn.nextInt();
k = scn.nextInt();
printkdistance(nn, inf, k);
t--;
}
}
}
but for two test cases its giving wrong answer because you need to print in sorted order
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.