Main.java uses unchecked or unsafe operations. Error

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();
}

}

@shahid.dhariwala
there might some warring in your code that why its not working.

@shahid.dhariwala
import java.util.*;
import java.util.LinkedList;
import java.util.Queue;
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<Integer> 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();
}
}

here is correct code
changes i made
1)import LinkedList and Queue
2)when passing a Hashset as parameter write it complete Hashset

1 Like

Thank you :slight_smile: “when passing a Hashset as parameter write it complete HashSet” was the real problem. Thanks for your quick response. :raised_hands: :raised_hands:

@shahid.dhariwala
please mark you doubt as resolved in my doubt section and rate me as well. :grinning:

1 Like