[LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)
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.
- Recursive approach is fine, implicit stack space does not count as extra space for this problem.
Example:
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL 这个题目因为是每一层之间的元素的关系, 所以很明显用BFS? 不知道为啥tag DFS, anyways, 我想的思路为, 利用每一层heig不一样, 然后设置一个pre, pre_h, 如果pre_h跟现在的heig相同, 那
表明是同一层, pre.next = node, 然后BFS即可. 12/03/2019 Update: 可以用Space:O(1), 设置start 和cur对每一层的来遍历,因为是perfect binary tree,所以如果有left child,肯定有right child。 1. Constraints
1) can be empty 2. Ideas BFS T: O(n) S: O(n)
按层遍历 T: O(n) S: O(1) 3. Code 1)
class Solution:
def connect(self, root):
pre, pre_h, queue = None, -1, collections.deque([(root, 0)])
while queue:
node, heig = queue.popleft()
if node:
if pre_h == heig:
pre.next = node
pre, pre_h = node, heig
queue.append(node.left)
queue.append(node.right)
2) S: O(1) for 116, perfect binary tree
if not root: return
head, start, cur = root, root, None
while start.left:
cur = start
while cur:
cur.left.next = cur.right
if cur.next:
cur.right.next = cur.next.left
cur = cur.next
start = start.left
return head
3) S: O(1) for 117, general binary tree, 用dummy来记录每一层之前的点, cur分别是一层中当时的点, root则为已经有next的parent的那一层的cur node.
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution(object):
def connect(self, root):
"""
:type root: Node
:rtype: Node
"""
dummy, head = Node(), root
cur = dummy
while root:
if root.left:
cur.next = root.left
cur = cur.next
if root.right:
cur.next = root.right
cur = cur.next
root = root.next
if not root:
root = dummy.next
cur = dummy
dummy.next = None
return head
4. Test cases
1
/ \
2 3
/ \ \
4 5 7
[LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)的更多相关文章
- leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- LeetCode OJ 117. Populating Next Right Pointers in Each Node II
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 117. 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 ...
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
随机推荐
- Win7/Win8/IIS7/IIS8配置ASP/ACCESS
1.在IIS信息服务管理器配置好站点后,配置ASP属性: a.IIS启用ASP 1.打开控制面板>>程序和功能>>“打开或关闭windows功能”,见下图 2.稍等片刻,出现一 ...
- C#访问MySQL数据库帮助类
MySQL数据库访问帮助类 1.项目添加引用官方MySQL动态库MySql.Data.dll 下载地址:MySql.Data.dll(也可以到官网下载动态库)项目添加引用 这里有一个Mysql帮助类的 ...
- ARC下带CF前缀的类型与OC类型转换
在对钥匙串操作时这个函数 OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef * __nullable CF_RETURNS_R ...
- vscode 修改(自定义)插件的快捷键
简单的使用了一下vscode,已经感受到了它的强大. 修改插件的快捷键: 1.快捷键command+k 按完之后再按command+s,弹出一个快捷键列表(code->首选项->键盘快捷方 ...
- Centos7.0 配置docker 镜像加速
在Docker Hub官网上注册帐号,即可下载使用仓库里的全部的docker镜像.而因为网络原因,国内的开发者没办法流畅的下载镜像,经常会出现下载中断的错误.解决方法就是使用国内的容器Hub加速服务, ...
- DependencyProperty属性介绍
1 DependencyProperty从属属性 1. 从属属性要定义为静态.为了在外部可以绑定,最好定义为Public 2. 从属属性实际上是取代了正常属性的存值变量 3. ...
- Notepad++如何关闭最近打开的文件的历史记录功能
Notepad++是Windows 操作系统下的一套非常有特色的自由软件的纯文字编辑器(许可证:GPL),有完整的中文化接口及支持多国语言编写的 功能(UTF8 技术).它的功能比Windows中的N ...
- CentOS6 防火墙配置
清空现有的规则 iptables -F iptables -P INPUT DROP iptables -I INPUT -m state --state RELATED , ESTABLISHED ...
- AD初体验
首先是因为想用51做个小项目,所以想到不如成这个机会把AD学一下吧,老师说我们这个专业无论画图还是电路设计都得精通,想想自己还是能力欠缺,到大三了才开始学习绘制 原理图. 好了废话不说,下面说说我的第 ...
- Pandas的concat方法
在此我用的concat作用是加入新的记录,存储数据来用过的,不知道数据量大时候,效率会怎样 # 使用pandas来保存数据 df1 = pd.DataFrame([poem], columns=['p ...