Please help Can’t figure out the problem
Output
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
import java.util.*;
public class Main {
public static Scanner sc = new Scanner(System.in);
private static class BinaryTree
{
private Node root;
private int size;
private class Node
{
int data;
Node left;
Node right;
int level;
Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
Node(int data, Node parent)
{
this.data = data;
this.left = null;
this.right = null;
if (parent == null)
{
this.level = 1;
} else
{
this.level = parent.level + 1;
}
}
}
BinaryTree()
{
this.root = this.takeInput();
}
private Node takeInput()
{
Queue<Node> q = new LinkedList<Node>();
Node node = new Node(sc.nextInt());
q.add(node);
while (!q.isEmpty())
{
Node temp = q.poll();
int child1 = sc.nextInt();
int child2 = sc.nextInt();
if (child1 != -1)
{
temp.left = new Node(child1,temp);
q.add(temp.left);
}
if (child2 != -1)
{
temp.right = new Node(child2,temp);
q.add(temp.right);
}
}
return node;
}
private void rightView()
{
HashSet<Integer> hm = new HashSet<>();
this.rightView(this.root,hm);
}
private void rightView(Node node,HashSet hm)
{
if(node==null)
{
return;
}
if(!hm.contains(node.level))
{
System.out.print(node.data+" ");
hm.add(node.level);
}
this.rightView(node.right,hm);
this.rightView(node.left,hm);
return;
}
}
public static void main(String args[]) {
BinaryTree bt1 = new BinaryTree();
bt1.rightView();
}
}
“when passing a Hashset as parameter write it complete HashSet” was the real problem. Thanks for your quick response.
