版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011296723/article/details/81775524
《剑指offer—面试题32:从上到下打印二叉树》
注明:仅个人学习笔记
方法一:—————————————————————————–
import java.util.ArrayList;
/**
*
*
* 从上到下打印树节点,层序遍历
*
*/
public class FromTopToEndPrintTree32
{
public static ArrayList PrintFromTopToBottom(TreeNode root)
{
// 用于存放层序遍历树的节点的值
ArrayList<Integer> order = new ArrayList<Integer>();
// 如果树空,则返回空order
if (root == null)
{
System.out.println("输入的树为空,请重新输入");
return order;
}
// 如果树只有根节点一个节点
if (root.left == null && root.right == null)
{
System.out.println("输入的树中只有一个节点");
order.add(root.value);
return order;
}
// 用于存放层序遍历的节点序列
ArrayList<TreeNode> list = new ArrayList<TreeNode>();
TreeNode node = root;
// 先将根节点放入遍历链表
if (node != null)
{
list.add(node);
}
int i = 0;
// 如果当前节点存在左右任一节点,那就添加到遍历链表中
while (node.left != null || node.right != null)
{
if (node.left != null)
{
list.add(node.left);
}
if (node.right != null)
{
list.add(node.right);
}
i++;
node = list.get(i);// 当前节点指向遍历链表中的下一节点,这样刚好满足层序遍历的遍历方式
}
// 取出遍历链表中节点值存入遍历值链表
for (int j = 0; j < list.size(); j++)
{
order.add(list.get(j).value);
}
return order;
}
public static void main(String[] args)
{
// 因为写的BinaryTree是构建完全二叉树,无法测试所有节点都只有右节点的情况,无法测试所有节点都只有左节点的情况
int[] array = { 8, 6, 10, 5, 7, 9, 11 };
BinaryTree tree = new BinaryTree(array);
tree.createBinTree();
ArrayList<Integer> order = PrintFromTopToBottom(tree.root);
for (int j = 0; j < order.size(); j++)
{
System.out.print(order.get(j) + ", ");
}
}
}
构建完全二叉树的代码
package com.chapter4.code;
import java.util.LinkedList;
/**
*
* 构建 完全二叉树
*/
import java.util.List;
public class BinaryTree
{
public int[] array;
public TreeNode root;
public BinaryTree(int[] array)
{
this.array = array;
}
// public static int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// 用List存放TreeNode
public List<TreeNode> nodeList = null;
public TreeNode createBinTree()
{
nodeList = new LinkedList<TreeNode>();
// 将一个数组的值依次转换为TreeNode节点
for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++)
{
nodeList.add(new TreeNode(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++)
{
// 左孩子
nodeList.get(parentIndex).left = nodeList.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).right = nodeList.get(parentIndex * 2 + 2);
}
// 最后一个父节点单独处理:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).left = nodeList.get(lastParentIndex * 2 + 1);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1)
{
nodeList.get(lastParentIndex).right = nodeList.get(lastParentIndex * 2 + 2);
}
root = nodeList.get(0);
return root;
}
}
方法二:—————————————————————————–
// 二叉树的层序遍历
public void layerorder(TreeNode root, ArrayList list)
{
if (root == null)
{
return;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (!queue.isEmpty())
{
TreeNode n = queue.poll();
list.add(n.value);
if (n.left != null)
{
queue.add(n.left);
}
if (n.right != null)
{
queue.add(n.right);
}
}
}