Day 23: BST Level-Order Traversal In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 23: BST Level-Order Traversal in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use โ€œCtrl+Fโ€ To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A โ€œFindโ€ Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 23: BST Level-Order Traversal โ€“ Hacker Rank Solution

Day 23: BST Level-Order Traversal โ€“ Hacker Rank Solution

Problem:

Day 23: BST Level-Order Traversal โ€“ Hacker Rank Solution
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author Techno-RJ
 *
 */
public class Day23BSTLevelOrderTraversal {
	static class Node {
		Node left, right;
		int data;

		Node(int data) {
			this.data = data;
			left = right = null;
		}
	}

	static void levelOrder(Node root) {
		if (root == null)
			System.out.println("nothing to display");
		Queue<Node> q = new LinkedList<Node>();
		q.add(root);
		while (!q.isEmpty()) {
			Node node = (Node) q.poll();
			System.out.print(node.data + " ");
			if (node.left != null)
				q.add(node.left);
			if (node.right != null)
				q.add(node.right);

		}
	}

	public static Node insert(Node root, int data) {
		if (root == null) {
			return new Node(data);
		} else {
			Node cur;
			if (data <= root.data) {
				cur = insert(root.left, data);
				root.left = cur;
			} else {
				cur = insert(root.right, data);
				root.right = cur;
			}
			return root;
		}
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		Node root = null;
		while (T-- > 0) {
			int data = sc.nextInt();
			root = insert(root, data);
		}
		sc.close();
		levelOrder(root);
	}
}

34 thoughts on “Day 23: BST Level-Order Traversal In Java | 30 Days Of Code | Hackerrank Programming Solutions”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker๐Ÿ™.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock