一、层次化遍历说明
层次化遍历:abcdefghij
二、层次化遍历代码
思想:采用队列先进先出的特性来实现
public static void levelTraversal(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> treeNodeQueue=new LinkedBlockingQueue<>();
treeNodeQueue.add(root);
while (!treeNodeQueue.isEmpty())
{
TreeNode currentNode=treeNodeQueue.poll();
System.out.println(currentNode.getValue());
if(currentNode.getLeft()!=null) {
treeNodeQueue.add(currentNode.getLeft());
}
if(currentNode.getRight()!=null) {
treeNodeQueue.add(currentNode.getRight());
}
}
}