Binary Tree Representation (Java)
Learning Traversals DSA practice problem on Onlearn.
Difficulty: easy.
Topics: How is a binary tree structured and implemented in Java?, Binary Tree, Tree Construction, Traversal, In-order Traversal, node structure, node operations, general programming, binary tree, tree traversal, Node, Pointers & References.
Problem Statement You are given a list of integers representing the level order traversal of a binary tree. In this representation, the nodes are filled level by level, from left to right. A value of 1 indicates that the specific position is null (no node exists at that position). Your task is to: 1. Construct the Binary Tree using the given level order array. 2. Perform an In order Traversal (Left $\rightarrow$ Root $\rightarrow$ Right) of the constructed tree. 3. Print the values obtained from the traversal. Note: The binary tree should be implemented using a node structure (e.g., a class with data, left, and right pointers). Input Specification A single line containing space separated integers representing the level order traversal of the tree. The value 1 represents a null node. Output Specification A single line containing space separated integers representing the In order traversal of the constructed binary tree. (Do not print 1 in the output). Constraints $1 \leq$ Number of nodes $\leq 10^4$ $1 \leq$ Node values $\leq 10^5$ (excluding the 1 placeholder) The input will always represent a valid binary tree structure. Sample Test Cases Sample Input 1 Sample Output 1 Explanation: The tree constructed is: 1 / \ 2 3 In order traversal visits Left (2), then Root (1), then Right (3). Sample Input 2 Sample Output 2 Explanation: The tree constructed is: 1 / \ 2 3 / \ 4 5 In order traversal: 4 $\rightarrow$ 2 $\rightarrow$ 5 $\rightarrow$ 1 $\rightarrow$ 3. Sample Input 3 Sample Output 3 Explanation: The tree is right skewed: 1 \ 2 \ 3 In order traversal: 1 $\rightarrow$ 2 $\rightarrow$ 3. Difficulty Level Easy