【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link:
https://oj.leetcode.com/problems/binary-tree-level-order-traversal/
Traverse the tree level by level using BFS method.
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a list of lists of integers
def levelOrder(self, root):
"""
BFS from the root.
For each level, insert a list of values into the result
"""
res = []
if not root:
return res
q = [root]
while q:
new_q = []
res.append([n.val for n in q])
for node in q:
if node.left:
new_q.append(node.left)
if node.right:
new_q.append(node.right)
q = new_q
return res
【LeetCode OJ】Binary Tree Level Order Traversal的更多相关文章
- 【LeetCode OJ】Binary Tree Level Order Traversal II
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...
- LeetCode OJ 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode OJ 102. Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- LeetCode OJ:Binary Tree Level Order Traversal II(二叉树的层序遍历)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode OJ:Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【Leetcode】【Easy】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【题解】【BT】【Leetcode】Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- abap调vb写的dll实现电子天平的读数(带控件版)
废话不多说,直接上. 鉴于abap调研的dll文件需要在wins注册,自己尝试过delphi和C#感觉不是很好,最后毅然选择了VB来写 因为需要用到MScomm控件,所以对于将要写的dll需要带for ...
- C++迟后联编和虚函数表
先看一个题目: class Base { public: virtual void Show(int x) { cout << "In Base class, int x = & ...
- ajax的探究与使用
前端必须掌握ajax,这是几乎所有前端招聘都会要求的一项. 但其实ajax也就是一种异步请求的技术,没有什么很深的东西,不过接触ajax很长一段时间了,早该整理下ajax的学习和使用: PART1: ...
- react参考项目001
https://github.com/chen2009277025/webpack-ant-design-demo https://github.com/cobish/cobish.github.io ...
- javascript选择排序
function selectionSort(arr){ var index,value; for(var i = 0;i < arr.length;i ++){ index = i; //先记 ...
- HDU--1233--还是畅通工程--并查集
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- MySQL5.6 GTID、多线程复制
MySQL5.6新特性GTID.多线程复制 在Oracle发布MySQL5.6看到众多新特性之后很兴奋,包括对复制的改进.在MySQL5.5半同步复制之后MySQL5.6又引入GTID.多线程复制,在 ...
- 面试复习(C++)之堆排序
#include <iostream> using namespace std; void Maxheap(int *a,int i,int heapSize)//最大数调整 { +;// ...
- 安装 mbed os 开发环境yotta
feature: 采用Python编写, Pip 包管理 CMake, the build system that yotta usesa compiler, to actually compile ...
- 更新App版本的流程
上班一年了还没有自己打包上传过APP,周五下班时项目经理手把手教了我一遍,我大致把流程在这里回顾一下: 1.首先要将svn上的代码拷贝一份到分支上,用终端操作:svn cp https://192.1 ...