tree related problems (update continuously)
leetcode Binary Tree Level Order Traversal
这道题是要进行二叉树的层次遍历。对于层次遍历,最简单直观的办法就是进行BFS。于是我们仅仅须要维护一个队列就能够了,队列里面的元素须要记录该节点的内容和节点所在的层。依次从队列中取出节点进行扩展就能够了。
# 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):
if(root == None):
return [] queue = []
queue.append((0, root)) ans = []
cur = 0
cur_nodes = []
while len(queue) != 0:
level, fa = queue.pop(0)
if(level == cur):
cur_nodes.append(fa.val)
else:
ans.append(cur_nodes)
cur_nodes = []
cur_nodes.append(fa.val)
cur = level
if fa.left != None:
queue.append((level+1, fa.left))
if fa.right != None:
queue.append((level+1, fa.right))
ans.append(cur_nodes)
return ans
tree related problems (update continuously)的更多相关文章
- dp related problems (update continuously)
Leetcode Maximum Product Subarray 这个问题是说给一个整数数组.求最大连续子阵列产品. 纠结了包括阵列中的很长一段时间0而如何处理负数,关键的事实是,负治疗,所以我们录 ...
- MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems
早晨宁波那边的IT人员打电话告知数据库无法访问了.其实我在早晨也发现Ignite监控下的宁波的数据库服务器出现了异常,但是当时正在检查查看其它服务器发过来的各类邮件,还没等到我去确认具体情 ...
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree
2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- SPOJ QTREE Query on a tree --树链剖分
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
- spoj 375 Query on a tree(树链剖分,线段树)
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Sub ...
- cmd & tree & bash
cmd & tree & bash bug E: Unable to locate package tree solution # 1. update $ sudo apt-get u ...
- UVA - 12424 Answering Queries on a Tree(十棵线段树的树链剖分)
You are given a tree with N nodes. The tree nodes are numbered from 1 to N and have colors C1, C2,. ...
随机推荐
- MacPorts的安装和使用
1.安装 MacPorts的官方网站:http://www.macports.org/install.php 有dmg安装和源代码安装两种方式,下载dmg格式一步步安装即可 2.使用 更新ports ...
- spring的事务传播与隔离
propagation 事务的传播属性: 1.PROPAGATION_REQUIRED(*-required):支持当前事务,如果当前没有事务,就新建一个事务.(最常见的选择) 2.PROPAGATI ...
- 【CF1023B】Pair of Toys(解方程)
题意:给定n个玩具要你选出两个玩具求出k的价值,第i个玩具的价值为i.若是没有选择方案,输出0 补充:玩具A与玩具B 和 玩具B和玩具A 是同一种选择 n,k<=1e14 思路:列出式子,解不等 ...
- 【BZOJ4034】T2(树链剖分)
题意: 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增 ...
- 相关分析 BZOJ 4821
相关分析 [问题描述] Frank对天文学非常感兴趣,他经常用望远镜看星星,同时记录下它们的信息,比如亮度.颜色等等,进而估算出星星的距离,半径等等.Frank不仅喜欢观测,还喜欢分析观测到的数据.他 ...
- Scrapy学习-17-暂停和重启
Scrapy爬虫暂停和重启 在当前项目下新建一个用于存储中间过程变量的目录 注意不同的spider不能共用一个目录 同一个spider每次运行时也必须使用新的目录 mkdir <spider_p ...
- Reactjs 的 PropTypes 使用方法
propTypes 使用來規範元件Props的型別與必需狀態 var Test = React.createClass({ propTypes: { // required requiredFunc: ...
- CSS兼容IE6 IE7 IE8 IE9 Firefox的总结
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- How to Mount a USB Drive in Ubuntu
Read more : http://www.ehow.com/how_6762235_mount-usb-drive-ubuntu.html Most USB drives will automou ...
- Java NIO中的Buffer类
Buffer 缓冲,用于批量读写数据 Buffer是一个抽象类,基本数据类型都有实现类:XxxBuffer,比如ByteBuffer.CharBuffer.IntBuffer.DoubleBu ...