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...)的更多相关文章

  1. 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 ...

  2. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  3. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. 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 ...

  5. 【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 ...

  6. LeetCode OJ 117. Populating Next Right Pointers in Each Node II

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. java高级---->Thread之ExecutorService的使用

    今天我们通过实例来学习一下ExecutorService的用法.我徒然学会了抗拒热闹,却还来不及透悟真正的冷清. ExecutorService的简单实例 一.ExecutorService的简单使用 ...

  2. c++ 用new创建二维数组~创建指针数组【转】

    #include <iostream> using namespace std; void main() { //用new创建一个二维数组,有两种方法,是等价的 //一: ] = ][]; ...

  3. Ubuntu 14.04 DNS 配置

    最近得到一个比较好用的DNS,每次重启后都修改DNS配置文件 /etc/resolv.conf 重启就会失效 从网上得知 /etc/resolv.conf中的DNS配置是从/etc/resolvcon ...

  4. jQuery事件处理(五)

    对原生js不熟悉看jQuery会困难很多.后续需要更多的关注下原生js jQuery封装之后的事件触发,其中一个分支(处理普通事件)是通过:elem.addEventListener( type, e ...

  5. 题目1162:I Wanna Go Home(最短路径问题进阶dijkstra算法))

    题目链接:http://ac.jobdu.com/problem.php?pid=1162 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  6. sencha touch 实现安卓toast效果(2013-9-26)

    如图中效果,代码很简单.就是利用st的loadmask来实现,可以看看api实现其他效果. /*添加消息提示组件*/ masked: { xtype: 'loadmask', cls: 'messag ...

  7. gitlab无法push或clone的错误:JWT::DecodeError (Nil JSON web token): lib/gitlab/workhorse.rb:120:in `verify_api_request!'

    使用源码安装的方式升级gitlib7.14到gitlab-8.13.5中文版,然后push的时候报错: 错误信息如下: Started GET "/gitlab/hushizhi/gitla ...

  8. cadence allegro 封装焊盘编号修改 (引脚编号修改)

    1. 打开dra文件在find里面 off all  然后只点击text 2.点击需要更改的焊盘 3.菜单栏edit - text 4.弹出窗口修改即可 注意: 按照网上的其他操作并没有执行步骤1操作 ...

  9. 【CF860E】Arkady and a Nobody-men 长链剖分

    [CF860E]Arkady and a Nobody-men 题意:给你一棵n个点的有根树.如果b是a的祖先,定义$r(a,b)$为b的子树中深度小于等于a的深度的点的个数(包括a).定义$z(a) ...

  10. Android sd卡log日志

    import android.os.Environment; import android.util.Log; import java.io.File; import java.io.FileOutp ...