二叉树的广度遍历
This commit is contained in:
parent
e3f0cf6148
commit
880d458212
|
|
@ -0,0 +1,50 @@
|
||||||
|
package leecode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二叉树的广度遍历
|
||||||
|
*/
|
||||||
|
public class No102 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<List<Integer>> levelOrder(TreeNode root) {
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
if (root == null){
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
Queue<TreeNode> queue = new LinkedBlockingQueue<>();
|
||||||
|
queue.offer(root);
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
//当前层数的元素个数
|
||||||
|
int qSize = queue.size();
|
||||||
|
List<Integer> layerList = new ArrayList<>();
|
||||||
|
while (qSize > 0){
|
||||||
|
qSize --;
|
||||||
|
TreeNode node = queue.poll();
|
||||||
|
layerList.add(node.val);
|
||||||
|
if (node.left != null){
|
||||||
|
queue.offer(node.left);
|
||||||
|
}
|
||||||
|
if (node.right != null){
|
||||||
|
queue.offer(node.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.add(layerList);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode left;
|
||||||
|
TreeNode right;
|
||||||
|
TreeNode(int x) { val = x; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue