【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 ...
随机推荐
- Linux下如何查看自己的服务器有没有无线网卡
还是实验室那台破服务器,连不上网.有没有界面,所以想着如何用一些命令来链接上热点. 当然,在linux下链接wifi没有win下那么一点就好了! 首先我们需要的基本条件就是: 服务器上有无线网卡.[r ...
- DataOdinalRegression
clear name={'SCV1V1','SVC1VA','SVR','CSSVC','SVMOP','NNOP','ELMOP','GPOR','KDLOR','POM',... 'NNPOM', ...
- 关于SQL语句优化的一个问题
今天写了一个很简单的存储过程,结果一执行,40多秒,后来调整了一句话写法,瞬间出来,其实差别不大,如下: select item_no=vpc.ITEM_ID ,BL_QTY=sum(vpc.QTY_ ...
- H-The Cow Lineup(POJ 1989)
The Cow Lineup Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5367 Accepted: 3196 De ...
- 用js实现返回上一页
<a href="javascript :;" onClick="javascript :history.back(-1);">返回上一页</ ...
- ES mlockall作用——preventing that memory from being paged to the swap area
elasticsearch还有一个重要的参数bootstrap.mlockall,这个参数的目的是当你无法关闭系统的swap的时候,建议把这个参数设为true.防止在内存不够用的时候,elastics ...
- 【转】Nginx+php-fpm+MySQL分离部署详解
转:http://www.linuxidc.com/Linux/2015-07/120580.htm Nginx+php-fpm+MySQL分离部署详解 [日期:2015-07-26] 来源:Linu ...
- BZOJ1393 [Ceoi2008]knights
题意...上ceoi官网看吧... 首先打一下sg函数发现必胜态和必败态的分布位置是有规律的 于是我们只要知道最长步数的必胜态和最长步数的必败态哪个更长就可以了 然后再打一下步数的表...发现必败态的 ...
- RemoveDPC
HOOKSSDT中加入了DPC之后 要取消DPC 首先找到DPCHookSSDT.sys的基地址和大小 通过枚举所有DPC的地址 将在范围之内的DPC定时器全部移除 枚举DPC: WinXP: 1. ...
- 【第53套模拟题】【递推】【RMQ】【二进制】【分块】
题目:(开始自己描述题目了...) 第一题大意: 求1~n的所有排列中逆序对为k个的方案数,输出方案数%10000,n<=1000. 解:这道题一个递推,因为我基本上没怎么自己做过递推,所以推了 ...