LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
'''
Created on Nov 19, 2014 @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
'''
# 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):
stack=[]
if(root==None): return
stack.append(root)
while(len(stack)!=0):
for ix in range(len(stack)-1):
stack[ix].next=stack[ix+1]
stack[-1].next=None
cntOfLastLevel=len(stack)
for ix in range(cntOfLastLevel):
if (stack[0].left!=None):stack.append(stack[0].left)
if (stack[0].right!=None):stack.append(stack[0].right)
stack.remove(stack[0]) class Solution2:
# @param root, a tree node
# @return nothing
def connect(self, root):
if(root==None): return
head_high=cursor_high=root
head_low = cursor_low=None while(cursor_high.left!=None):
head_low = cursor_low=cursor_high.left cursor_low.next=cursor_high.right
cursor_low=cursor_low.next
while(cursor_high.next!=None):
cursor_high=cursor_high.next
cursor_low.next=cursor_high.left
cursor_low=cursor_low.next
cursor_low.next=cursor_high.right
cursor_low=cursor_low.next
cursor_low.next=None head_high=cursor_high=head_low
题目和代码如上所述,这个题比较有意思的地方是,这个数据结构有点像数据库索引的结构,上述代码实现了两种方法,两种方法都是层级遍历,时间复杂度都是O(N),但空间复杂度不一样,实现难度也不一样。
1. 第一种更为简单,但使用了额外的空间用于存储上一层节点,最大空间复杂度为所有叶子节点的大小之和。所以类似这种算法如果用于建立DB索引的话,恐怕内存就要爆掉了,第二种方法则没有问题。
2. 第二种稍复杂,但是空间复杂度只有O(1),也就是无需任何额外内存。实现方法是使用两个游标和1个标志位,两个游标用于并行遍历两行nodes,1个标志位用于标记下面那行的head。
两游标并行往前走,会同时走到各自行的结尾,这时两游标各自下移到下一行开始(这就是为啥要标记那行的head)然后重复上面的过程继续往前走,当没有下一行时停止(第二个游标没得指了),请自行脑补2个游标遍历所有nodes并且加链接的过程。
LEETCODE —— Populating Next Right Pointers in Each Node的更多相关文章
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [LeetCode] 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 II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- LeetCode: Populating Next Right Pointers in Each Node 解题报告
Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode { Tree ...
- [LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
随机推荐
- 每天一点 js join 函数
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- SQL注入:突破关键字过滤
一直以来都以为只有空格,tab键和注释符/**/可以用来切割sql关键字,段时间在邪八看了风迅cms注入漏洞那篇帖子,才知道原来回车也可以用来作为分割符(以前竟然没有想到,真是失败).回车的ascii ...
- Hive的API的说明
之前通过命令行的界面可以操作Hive,可是在实际的生产环境中,往往都是需要写API的,因此对Hive的API简单的列举了一下.并对Hive进行了一个简单的封装.具体的封装可以参考github网站主页: ...
- js之事件冒泡和事件捕获详细介绍
(1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...
- MVC 支持同名路由,不同命名空间
有时候我们会碰到两个项目合在一起,那么必然会碰到两个同名的controller,其实MVC在注册路由,添加Route的时候可以指定当前规则解析那个命名空间下的所有Controller. 注:Contr ...
- DataTable的子查询--DataTable.Select()
由于需要,在sql查询获得结果集之后,又需要对该结果再进行筛选一次,若重新从sql中查询会浪费资源,因此特地去查阅了一些资料,学会了用DataTable进行子查询. 在.Net Framework 2 ...
- jenkins配置
自动化测试机器172,27.14.22 IP 一.jenkins要先登录——>点击JCF_Automation——>点击左边配置 二.环境变量赋值就不会把进程杀掉
- 深入理解JavaScript系列:史上最清晰的JavaScript的原型讲解
一说起JavaScript就要谈的几个问题,原型就是其中的一个.说了句大话,史上最清晰.本来是想按照大纲式的行文写一下,但写到后边感觉其实就一个概念,没有什么条理性,所以下面就简单按照概念解释的模式谈 ...
- 【Python】使用正则表达式实现计算器练习
已知有以下这样一个不太友好的公式: 1 - 2 * ( (60-30 +(-9-2-5-2*3-5/3-40*4/2-3/5+6*3) * (-9-2-5-2*5/3 + 7 /3*99/4*2998 ...
- Longest Increasing Path in a Matrix -- LeetCode 329
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...