Comments (2)
解题思路
- 将临时列表中存入根node, 开始遍历,
- 将临时列表中所有node的值存入结果列表中,
- 清空临时列表, 将下次循环需要遍历的node的子节点存入临时列表中.
代码
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() { }
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public static List<List<Integer>> func(TreeNode root) {
List<TreeNode> tmpList = new ArrayList<>();
if (null != root) tmpList.add(root);
List<List<Integer>> x = new ArrayList<>(); // 存放结果
while (tmpList.size() > 0) {
// 将本层的节点复制到新list中, 并将老list清空
List<TreeNode> tmp = new ArrayList();
tmp.addAll(tmpList);
tmpList.clear();
List<Integer> y = new ArrayList<>();
for (TreeNode node : tmp) {
// 将本层的值统计
y.add(node.val);
// 将本层的节点按顺序添加, 为下一次循环
if (null != node.left)
tmpList.add(node.left);
if (null != node.right)
tmpList.add(node.right);
}
x.add(y);
}
return x;
}
from pisecespeng.record.me.
LeetCode题解
解题思路
- 将根node存入队列, 开始遍历,
- 将node推出队列, node值存入结果列表,
- 将node左右节点存入队列, 作为下次循环数据.
代码
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() { }
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public static List<List<Integer>> func(TreeNode root) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (root == null) {
return ret;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> level = new ArrayList<Integer>();
int currentLevelSize = queue.size();
for (int i = 1; i <= currentLevelSize; ++i) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
ret.add(level);
}
return ret;
}
from pisecespeng.record.me.
Related Issues (20)
- 两数相加 HOT 2
- 买卖股票的最佳时机 HOT 3
- 买卖股票的最佳时期2 HOT 3
- 二叉树的最大深度 HOT 2
- 二进制求和 HOT 4
- 从尾到头打印链表 HOT 2
- 位1的个数 HOT 4
- 删除排序数组中的重复项 HOT 2
- 删除链表的倒数第N个节点 HOT 3
- 加一 HOT 2
- 反转字符串 HOT 2
- 反转链表 HOT 3
- 只出现一次的数字 HOT 2
- 合并两个有序数组 HOT 3
- 合并两个有序链表 HOT 2
- 回文数 HOT 2
- 回文链表 HOT 3
- 复制带随机指针的链表 HOT 2
- 外观数列 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pisecespeng.record.me.