我有以下代码在每个级别打印二叉树。但是我不想要当前的输出,我需要额外的金字塔空间结构:
import java.util.LinkedList;
import java.util.Queue;
public class LevelOrder
{
static class Node
{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left = null;
right =null;
}
}
static void printLevelOrder(Node root)
{
if(root == null)
return;
Queue<Node> q =new LinkedList<Node>();
q.add(root);
while(true)
{
int nodeCount = q.size();
if(nodeCount == 0)
break;
while(nodeCount > 0)
{
Node node = q.peek();
System.out.print("("+node.data + ")");
q.remove();
if(node.left != null)
q.add(node.left);
if(node.right != null)
q.add(node.right);
if(nodeCount>1){
System.out.print(", ");
}
nodeCount--;
}
System.out.println();
}
}
public static void main(String[] args)
{
Node root = new Node(6);
root.left = new Node(3);
root.right = new Node(2);
root.left.left = new Node(8);
root.left.right = new Node(9);
root.right.right = new Node(-2);
root.right.left = new Node(3);
printLevelOrder(root);
}
}
我的当前输出为AS FOLLOWS:
(6)
(3), (2)
(8), (9), (3), (-2)
但我需要这样的东西:
(6)
(3), (2)
(8), (9), (3), (-2)
如何以有效的方式打印上述输出所需的空格。请帮忙。
答案 0 :(得分:0)
实际上你得到了正确的输出,但你没有得到正确格式的输出。要做到这一点,你必须将相应的输出显示为三角形。我必须用我的代码完成它。如果你得到任何问题,随时发表评论。我一定会帮助你。
试用此代码: -
import java.util.LinkedList;
import java.util.Queue;
public class LevelOrder
{
static class Node
{ int data; Node left; Node right;
Node(int data)
{
this.data = data;
left = null;
right = null;
depth = 1;
}
}
static void printLevelOrder(Node root)
{
if (root == null)
return;
Queue < Node > q = new LinkedList < Node > ();
q.add(root);
while (true)
{
int nodeCount = q.size();
if (nodeCount == 0)
break;
while (nodeCount > 0)
{
Node node = q.peek();
System.out.print("(" + node.data + ")");
q.remove();
if (node.left != null)
q.add(node.left);
if (node.right != null)
q.add(node.right);
if (node.depth != null)
System.out.print(" ");
if (nodeCount > 1)
{
System.out.print(", ");
}
nodeCount--;
}
System.out.println();
}
}
public static void main(String[] args)
{
Node root = new Node(6);
root.left = new Node(3);
root.right = new Node(2);
root.left.left = new Node(8);
root.left.right = new Node(9);
root.right.right = new Node(-2);
root.right.left = new Node(3);
printLevelOrder(root);
}
}
答案 1 :(得分:0)
这是你正在寻找的,我介绍了一种方法,计算树的深度,并根据你打印节点的级别,我相应地介绍空格。
import java.util.LinkedList;
import java.util.Queue;
public class Main
{
static class Node
{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left = null;
right =null;
}
}
static int depthOfTree(Node root, int d) {
if(root == null) {
return d;
}
int left = d;
int right = d;
if(root.left != null) {
left = depthOfTree(root.left, d+1);
}
if(root.right != null) {
right = depthOfTree(root.right, d+1);
}
return Math.max(left, right);
}
static void printLevelOrder(Node root, int depth)
{
if(root == null)
return;
Queue<Node> q =new LinkedList<Node>();
q.add(root);
while(true)
{
int nodeCount = q.size();
if(nodeCount == 0)
break;
for(int i=0; i<depth; i++) {
System.out.print(" ");
}
while(nodeCount > 0)
{
Node node = q.peek();
System.out.print("("+node.data + ")");
q.remove();
if(node.left != null)
q.add(node.left);
if(node.right != null)
q.add(node.right);
if(nodeCount>1){
System.out.print(", ");
}
nodeCount--;
}
depth--;
System.out.println();
}
}
public static void main(String[] args)
{
Node root = new Node(6);
root.left = new Node(3);
root.right = new Node(2);
root.left.left = new Node(8);
root.left.right = new Node(9);
root.right.right = new Node(-2);
root.right.left = new Node(3);
int d = depthOfTree(root, 1);
//System.out.println(d);
printLevelOrder(root, d);
}
}
希望这有帮助!