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 ...
随机推荐
- flexbox实现不等宽不等高的瀑布流布局
第一次做不等宽不等高的瀑布流布局,刚开始企图用ccs3的column属性+flexbox来实现,瞎捣鼓半天都没有能弄好, 弱鸡哭晕在厕所(┬_┬),气的午饭都没有吃. 后来逼着自己冷静下来,又捣鼓了1 ...
- C#3.0 扩展方法
扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.对于用 C# 和 Visual ...
- 【python】属性
在python中,一切皆是对象(object),对象拥有很多属性(arrtribute) 属性分2种 类属性(class attribute):类自身定义or继承 对象属性(object attrib ...
- Kafka的安装和部署及测试
1.简介 大数据分析处理平台包括数据的接入,数据的存储,数据的处理,以及后面的展示或者应用.今天我们连说一下数据的接入,数据的接入目前比较普遍的是采用kafka将前面的数据通过消息的方式,以数据流的形 ...
- C#和Java在重写上的区别
C# class A { public string Get1() { return "A1"; } public virtual string Get2() { return & ...
- Android数据持久化技术 — — —文件存储
文件保存 package com.example.datastroredtest; import android.app.Activity;import android.os.Bundle;impor ...
- 初识reactJs 相关
喽了一眼阮一峰老师的react文章,感觉写的挺棒,这篇只是按照自己思路屡一遍,纯属自学笔记,不承担社会暴乱责任.前几天,打算学vuejs,师兄给了一句话的点播,感觉很醍醐灌顶.总结下,所 ...
- JS在火狐浏览器下如何关闭标签?
首先,要确定火狐设置是否允许通过JS代码window.close()方法关闭标签. 确定方式如下: 在Firefox地址栏里输入 about:config 在配置列表中找到dom.allow_scri ...
- PCB Layout高速电路设计小知识点
1.单位:1mil = 0.0254mm 2.default线宽及线距(综合考虑高速电路性能及PCB板厂制程能力):6mil 3.差分走线特征阻抗:100ohm 4.3W原则,即线间距采用3倍线宽,多 ...
- 数据导出到excel
jsp页面: 数据table: <form action="/export.jsp" method="post" id="expform&quo ...