116. Populating Next Right Pointers in Each Node
Medium

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *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.

 

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

 

Example 1:

Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

 

Constraints:

  • The number of nodes in the given tree is less than 4096.
  • -1000 <= node.val <= 1000

First code cannot get the right value,but I can't find problem , need check

Second is from HH's C++ code ,I don't understand what &gt means

"""
# 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
#         """
#         if not root:return
#         nextLevelFirst=root
#         while(root.left):
#             nextLevelFirst=root.left
#             while(root):
#                 root.left.next=root.right
#                 if root.next!=None:root.right.next=root.next.left
#                 root=root.next
#             root=nextLevelFirst

class Solution(object):
    def connect(self, root):
        """
        :type root: TreeLinkNode
        :rtype: nothing
        """
        if not root : return None
        nextLevelFirst = root
        while root.left :
            nextLevelFirst = root.left
            while root :
                root.left.next = root.right
                if root.next != None: root.right.next = root.next.left
                root = root.next
            root = nextLevelFirst
// Author: Huahua
class Solution {
public:
  void connect(TreeLinkNode *root) {
    if (!root) return;
    if (!root-&gt;left || !root-&gt;right) return;
    root-&gt;left-&gt;next = root-&gt;right;
    if (root-&gt;next)
      root-&gt;right-&gt;next = root-&gt;next-&gt;left;
    connect(root-&gt;left);
    connect(root-&gt;right);
  }
};

 The above two methods all have problem I cannot solved now.

BONUS:

1.Perfect Binary Tree

perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level.

ADDS: The class  I want to create but facing problem of different Tree structure may cause Read Data problem can start 

from PBT.This regular input meet the requirement of Tree Class.

 

posted on 2019-12-05 09:03  黑暗尽头的超音速炬火  阅读(204)  评论(0)    收藏  举报