BinaryTreel Taking Input and Output
BinaryTreel Taking Input and Output
*;
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left, right;
while (!pendingNodes.isEmpty()) {
BinaryTreeNode<Integer> front = pendingNodes.poll();
return root;
}
public static void printLevelWise(BinaryTreeNode<Integer> root) {
if (root == null) {
return;
}
while (!pendingNodes.isEmpty()) {
BinaryTreeNode<Integer> frontNode = pendingNodes.poll();
if (frontNode == null) {
System.out.println();
if (!pendingNodes.isEmpty()) {
pendingNodes.add(null); // Null marker for the next level
}
} else {
System.out.print(frontNode.data + ":");
System.out.print("L:");
if (frontNode.left != null) {
pendingNodes.add(frontNode.left);
System.out.print(frontNode.left.data + ",");
} else {
System.out.print("-1,");
}
System.out.print("R:");
if (frontNode.right != null) {
pendingNodes.add(frontNode.right);
System.out.println(frontNode.right.data);
} else {
System.out.println("-1");
}
}
}
}
public static void main(String[] args) {
BinaryTreeNode<Integer> root = takeInputLevelWise();
printLevelWise(root);
}
}
import java.util.Scanner;
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left, right;
System.out.print(root.data + ":");
if (root.left != null) {
System.out.print("L" + root.left.data + ",");
}
if (root.right != null) {
System.out.print("R" + root.right.data);
}
System.out.println();
printTree(root.left);
printTree(root.right);
}
return root;
}