[牛客]有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。
来源:互联网 发布:手机乐园java手机版 编辑:程序博客网 时间:2024/06/05 23:44
有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。
给定二叉树的根结点root,请返回打印结果,结果按照每一层一个数组进行储存,所有数组的顺序按照层数从上往下,且每一层的数组内元素按照从左往右排列。保证结点数小于等于500。
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class TreePrinter {
public int[][] printTree(TreeNode root) {
int index = 0;
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> temp = new ArrayList<Integer>();
TreeNode left = root , right = root;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(root);
while(q.peek() != null){
if(q.peek().left != null){
q.add(q.peek().left);
left = q.peek().left;
}
if(q.peek().right != null){
q.add(q.peek().right);
left = q.peek().right;
}
TreeNode aNode = q.remove();
temp.add(aNode.val);
if(right == aNode){
right = left;
if(temp.size() > 0){
list.add(temp);
}
temp = new ArrayList<Integer>();
}
}
//转换数组
int[][] result = new int[list.size()][];
for(int i = 0 ; i < list.size() ; i++){
temp = list.get(i);
result[i] = new int[temp.size()];
for(int j = 0 ; j < temp.size() ;j++){
result[i][j] = temp.get(j);
}
}
return result;
}
}
----------------------------------带测试用例--------------------------------
package test;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class test {public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }} public int[][] printTree(TreeNode root) { if(root == null) return null; List<ArrayList<TreeNode>> list = new ArrayList<ArrayList<TreeNode>>(); ArrayList<TreeNode> temp = new ArrayList<TreeNode>(); LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); TreeNode last = root ; TreeNode nlast = root ; queue.push(root); while(queue.peek() != null ){ if(queue.peek().left != null){ queue.add(queue.peek().left); nlast = queue.peek().left; } if(queue.peek().right != null){ queue.add(queue.peek().right); nlast = queue.peek().right; } if(last == queue.peek()){ temp.add(queue.pop()); list.add(temp); temp = new ArrayList<TreeNode>(); last = nlast; }else{ temp.add(queue.pop()); } } int[][] result = new int[list.size()][]; int index = 0 ; for(ArrayList<TreeNode> node_list : list){ result[index] = new int[node_list.size()]; int index1 = 0; for(TreeNode node : node_list){ result[index][index1] = node.val; index1++; } index++; } return result; } public static void main(String[] a){ test t = new test(); TreeNode a1 = t.new TreeNode(1); TreeNode a2 = t.new TreeNode(2); TreeNode a3 = t.new TreeNode(3); TreeNode a4 = t.new TreeNode(4); TreeNode a5 = t.new TreeNode(5); TreeNode a6 = t.new TreeNode(6); TreeNode a7 = t.new TreeNode(7); TreeNode a8 = t.new TreeNode(8); a1.left=a2; a1.right=a3; a2.left=a4; a3.left=a5; a3.right=a6; a5.left=a7; a5.right=a8; t.printTree(a1); }}
1 0
- [牛客]有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。
- 有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。 给定二叉树的根结点root,请返回打印结果,
- 设计一个算法,按照层次打印这棵二叉树。
- 按照层次打印二叉树
- 按照层次打印二叉树算法C++实现
- 按照层次遍历并打印二叉树
- 按照层次遍历二叉树
- 层次打印二叉树
- 层次打印二叉树
- 层次打印二叉树
- 按照之字形打印二叉树
- 层次结构打印二叉树
- 按层次打印二叉树
- 二叉树层次遍历打印
- Python二叉树层次打印
- 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
- 构建一棵二叉树并按照层次遍历输出
- 牛客网 算法第一题 二叉树按层次打印问题
- ZOJ 1032Area 2
- 4-Embedded Training
- 栖霞建设走势
- c# 编写基站定位API接口解析
- Java直接控制打印机打印
- [牛客]有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树。
- 推荐 浙江省国税vpdn安装正确的方法 如何安装后缀为ocx的控件 由于无法验证发布者 ,所以windows 已经阻止次软件
- 实验二 阶乘while循环
- 失业66天(转载自zhao_yin的记事本)
- Java回调机制解读
- ajax最最常用的七个属性
- android毕设(2)
- 初识前端框架(Vue)打包遇到的坑~
- 《VR产业白皮书》