【LeetCode OJ】Populating Next Right Pointers in Each Node
Problem Link:
http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
Just traverse the tree from the root, level by level. Esay enough.
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
"""
We use a BFS from the root, so that we can traverse the tree level by level.
We need two queues, to store the nodes of this level and next.
"""
if root is None:
return
q = [root]
while q:
new_q = []
for i in xrange(len(q)-1):
q[i].next = q[i+1]
q[-1].next = None
for node in q:
if node.left:
new_q.append(node.left)
if node.right:
new_q.append(node.right)
q = new_q
【LeetCode OJ】Populating Next Right Pointers in Each Node的更多相关文章
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
- LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- LeetCode OJ 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode OJ 117. Populating Next Right Pointers in Each Node II
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
随机推荐
- struts2在pom.xml中的配置
<dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifac ...
- 各种浏览器css hack
转载 http://www.cnblogs.com/jikey/archive/2010/06/21/1761924.html IE都能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识 ...
- CSS 层叠及样式表来源
Web标准化运动的口号——分离.分离.分离. 在2003年的 SXSW 会议中, Steve Champeon 和 Nick Finck 做了一个名为“面向未来的全方位 Web 设计”的演讲,揭示了这 ...
- Div样式查看器
编写div属性时,经常需要尝试不同的样式,可以用Javascript写一个简单的div样式查看器,方便日常操作: <!DOCTYPE html> <html> <head ...
- 读取Cookie及Cookie所有属性操作方法
读取Cookie及Cookie所有属性操作方法 2013-08-04 22:21:43| 分类: 技术 | 标签:cookie |举报|字号 订阅 要把Cookie发送到客户端,Servle ...
- POC测试——原型验证,降低风险,IT系统销售工作之一
POC测试,即Proof of Concept,是业界流行的针对客户具体应用的验证性测试,根据用户对采用系统提出的性能要求和扩展需求的指标,在选用服务器上进行真实数据的运行,对承载用户数据量和运行时间 ...
- [css]邮件的写法
<style type="text/css"> /* Client-specific Styles */ #outlook a{paddin ...
- Rhel6-tomcat+nginx+memcached配置文档
理论基础: User - > web ->nginx ->tomcat1 ->*.jsp 80 8080 ↓ -> tomcat2 html ...
- python框架(flask/django/tornado)比较
一.对外数据接口 三者作为web框架,都是通过url映射对外的接口 flask:以decorator的形式,映射到函数中 django:以字典形式,映射到函数 tornado: 以字典形式,映射到类中 ...
- AIX查看内存卡槽
1.lscfg -vp|grep Processor 2.lscfg -vp|grep -p Memory