Leetcode 116. Populating next right pointers in each node I and II

题目1:

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

题目2:

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For 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

我们可以用类似level order traversal 以及 level zigzag traversa 的方法来直接解决这个更general的第二题l。本题只不过在输出每层的时候把本层的node从左向右用.next连起来。

 1 # Definition for binary tree with next pointer.
 2 # class TreeLinkNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 #         self.next = None
 8
 9 class Solution:
10     # @param root, a tree link node
11     # @return nothing
12     def connect(self, root):
13         if not root:
14             return
15         queue = [root]
16         while queue:
17             level = []
18             size = len(queue)
19             for i in range(size):
20                 node = queue.pop(0)
21                 level.append(node)
22                 if node.left:
23                     queue.append(node.left)
24                 if node.right:
25                     queue.append(node.right)
26
27             for i in range(len(level)-1):
28                 level[i].next = level[i+1]
29             level[-1].next = None
30
31         return
时间: 2024-10-29 04:03:50

Leetcode 116. Populating next right pointers in each node I and II的相关文章

leetcode 116 Populating Next Right Pointers in Each Node ----- java

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, al

[LeetCode] 116. 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, a

Java for LeetCode 116 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, al

LeetCode 116. Populating Next Right Pointers in Each Node

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 ver0: 1 class Solution { 2 public: 3 void connect(TreeLinkNode

LeetCode[Tree]: Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example, Given the following binary t

【56】116. Populating Next Right Pointers in Each Node

116. Populating Next Right Pointers in Each Node Description Submission Solutions Add to List Total Accepted: 118451 Total Submissions: 321021 Difficulty: Medium Contributors: Admin Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLi

Leetcode 树 Populating Next Right Pointers in Each Node II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II Total Accepted: 9695 Total Submissions: 32965 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could

Leetcode 树 Populating Next Right Pointers in Each Node

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node Total Accepted: 13657 Total Submissions: 39540 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }

leetcode -day14 Populating Next Right Pointers in Each Node I II

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