代码改变世界

[LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)

2018-07-13 07:33  Johnson_强生仔仔  阅读(218)  评论(0编辑  收藏  举报

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)
 1 class Solution:
 2     def connect(self, root):
 3         pre, pre_h, queue = None, -1, collections.deque([(root, 0)])
 4         while queue:
 5             node, heig = queue.popleft()
 6             if node:
 7                 if pre_h == heig:
 8                     pre.next = node
 9                 pre, pre_h = node, heig
10                 queue.append(node.left)
11                 queue.append(node.right)

 

2) S: O(1)    for 116, perfect binary tree

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        start = root
        while start:
            cur = start
            while cur:
                if cur.left:
                    cur.left.next = cur.right
                    if cur.next:
                        cur.right.next = cur.next.left
                cur = cur.next
            start = start.left
        return root

 

 

 

3) S: O(1)    for 117, general binary tree, 用dummy来记录每一层之前的点, cur分别是一层中当时的点, root则为已经有next的parent的那一层的cur node.

"""
# Definition for a Node.
class Node:
    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        dummy, start = Node(), root
        pre = dummy
        while start:
            cur = start
            while cur:
                if cur.left:
                    pre.next = cur.left
                    pre = cur.left
                if cur.right:
                    pre.next = cur.right
                    pre = cur.right
                cur = cur.next
            start = dummy.next # get the next start
            pre = dummy  # get the dummy
            pre.next = None   # remove the pointer from dummy
        return root

 

4. Test cases

 

     1
   /  \
  2    3
 / \    \
4   5    7