标签:tree eve node int result div == 代码 res
BFS广度遍历代码模板
/** 广度遍历代码模板 */ public class TestBFS { public List<List<Integer>> bsf(TreeNode root) { // 如果节点为空 if (root == null) { return null; } List<List<Integer>> result = new ArrayList<>(); Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { int size = queue.size(); List<Integer> level = new ArrayList<>(); for (int i = 0; i < size; i++) { TreeNode curNode = queue.poll(); if (curNode == null) { continue; } level.add(curNode.val); queue.add(curNode.left); queue.add(curNode.right); } if (!level.isEmpty()) { result.add(level); } } return result; } }
标签:tree eve node int result div == 代码 res
原文地址:https://www.cnblogs.com/gaopengpy/p/13294644.html