leetcood学习笔记-102-二叉树的层次遍历
题目描述:

方法一;
class Solution(object):
def levelOrder(self, root): if not root:
return []
ans = []
stack = [root]
while stack:
tem_stack = []
tem_ans = []
for i in stack:
tem_ans.append(i.val)
if i.left:
tem_stack.append(i.left)
if i.right:
tem_stack.append(i.right)
stack = tem_stack
ans.append(tem_ans)
return ans
java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ans = new ArrayList<> ();
if(root == null){
return ans;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> list = new ArrayList<>();
while(size > 0){
TreeNode curr = queue.poll();
list.add(curr.val);
size--;
if (curr.left != null){
queue.offer(curr.left);
}
if (curr.right != null){
queue.offer(curr.right);
}
}
ans.add(list);
}
return ans;
}
}
leetcood学习笔记-102-二叉树的层次遍历的更多相关文章
- leetcood学习笔记-107-二叉树的层次遍历二
题目描述: 方法一: class Solution(object): def levelOrderBottom(self, root): """ :type root: ...
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
- Java实现 LeetCode 102 二叉树的层次遍历
102. 二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 2 ...
- leetcode 102. 二叉树的层次遍历 及 103. 二叉树的锯齿形层次遍历
102. 二叉树的层次遍历 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / ...
- Leetcode 102 二叉树的层次遍历 Python
二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 ...
- Leetcode题目102.二叉树的层次遍历(队列-中等)
题目描述: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...
- LeetCode 102 二叉树的层次遍历
题目: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- LeetCode 102 ——二叉树的层次遍历
1. 题目 2. 解答 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level_data 和一个存放每一层节点的队列 node_queue. 如果根节点非空,根节点进队,然后循 ...
- leetcood学习笔记-226- 翻转二叉树
题目描述: 第一次提交: class Solution(object): def invertTree(self, root): """ :type root: Tree ...
随机推荐
- 项目案例之GitLab的数据迁移
项目案例之GitLab的数据迁移 链接:https://pan.baidu.com/s/1CgaEv12cwfbs5RxcNpxdAg 提取码:fytm 复制这段内容后打开百度网盘手机App,操作更方 ...
- 【hihoCoder】每周一题(从week 220开始)
2018/9/17-2018/9/23 week 220 push button I 题目链接:https://hihocoder.com/contest/hiho220/problem/1 有N个 ...
- Form与ModelForm中的插件使用
一.Form插件的使用 (一)widget参数 from .models import * from django import forms from django.forms import widg ...
- cboard进行访问,汉化
- 【leetcode】939. Minimum Area Rectangle
题目如下: Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from t ...
- 多媒体查询 @media 报错
You may not @extend an outer selector from within @media.You may only @extend selectors within the s ...
- openFrameworks Download
{ https://openframeworks.cc/zh_cn//download/ } 0.10.1 是最新发布的版本. 这个版本是修改了一些BUG的小版本,与版本 0.10.1100%兼容而且 ...
- Windows 获取windows密码
#include <iostream> #define Main main #define COLOR_GREEN system("color 2"); #includ ...
- RichViewEdit
RichViewEdit特殊操作 RichviewEdit 图文保存操作 首先要转换成stream后才能对RichviewEdit进行正确的读和写 function SaveRVFToField(rv ...
- Python语法基础03(if语句,while循环与for循环)
if语句:语法:单分支if 判断条件:语句块 执行过程:首先执行判断条件,当条件成立则执行判断条件下面的语句块,若条件不成立,则不执行 双分支if 判断条件:语句块1else:语句块2执行过程: 首先 ...