102:二叉树的层序遍历

This commit is contained in:
huangge1199 2021-07-30 13:40:00 +08:00
parent a42435bf84
commit f2aa974ff1
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,89 @@
//给你一个二叉树请你返回其按 层序遍历 得到的节点值 即逐层地从左到右访问所有节点
//
//
//
// 示例
//二叉树[3,9,20,null,null,15,7],
//
//
// 3
// / \
// 9 20
// / \
// 15 7
//
//
// 返回其层序遍历结果
//
//
//[
// [3],
// [9,20],
// [15,7]
//]
//
// Related Topics 广度优先搜索 二叉树
// 👍 945 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
//102:二叉树的层序遍历
public class BinaryTreeLevelOrderTraversal {
public static void main(String[] args) {
//测试代码
Solution solution = new BinaryTreeLevelOrderTraversal().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> result = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode temp = queue.poll();
list.add(temp.val);
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
result.add(list);
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,25 @@
<p>给你一个二叉树,请你返回其按 <strong>层序遍历</strong> 得到的节点值。 (即逐层地,从左到右访问所有节点)。</p>
<p> </p>
<p><strong>示例:</strong><br />
二叉树:<code>[3,9,20,null,null,15,7]</code>,</p>
<pre>
3
/ \
9 20
/ \
15 7
</pre>
<p>返回其层序遍历结果:</p>
<pre>
[
[3],
[9,20],
[15,7]
]
</pre>
<div><div>Related Topics</div><div><li></li><li>广度优先搜索</li><li>二叉树</li></div></div>\n<div><li>👍 945</li><li>👎 0</li></div>