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

问题: 给定一个二叉树,将树元素的 *next 指向该元素在树结构中的水平右边节点。

这是广度遍历的一个应用。可以借组队列结构实现广度遍历,求解题目。

思路:

  • 当队列中元素恰好是树种某一行的全部元素时,则给队列中每个节点的 *next 赋值为 列表中对应的下一个节点,其中特别地,最后一个元素*next 为 NULL。
  • 将队列中的元素全部弹出,并依次塞进他们的子节点,此时,队列中的元素恰好是树种下一行的全部元素,继续上一步操作。

将根节点塞进队列,即实现了上面思路的初始化。

 1     void connect(TreeLinkNode *root) {
 2
 3         if(root == NULL){
 4             return;
 5         }
 6
 7         list<TreeLinkNode*> queue;
 8
 9         queue.push_back(root);
10
11         while(queue.size() > 0 ){
12
13             // assign value to the next point of the node in queue.
14             list<TreeLinkNode*>::iterator q_iter;
15             for( q_iter = queue.begin() ; std::next(q_iter,1) != queue.end(); q_iter++){
16                 (*q_iter)->next = *std::next(q_iter,1);
17             }
18
19             // pop each node in the current row in the tree structure, and push the left and right childrens of them into queue.
20             while(queue.front()->next != NULL){
21                 TreeLinkNode* node = queue.front();
22                 queue.pop_front();
23
24                 if(node->left != NULL){
25                     queue.push_back(node->left);
26                 }
27
28                 if(node->right != NULL){
29                     queue.push_back(node->right);
30                 }
31             }
32
33             TreeLinkNode* node = queue.front();
34             queue.pop_front();
35
36             if(node->left != NULL){
37                 queue.push_back(node->left);
38             }
39
40             if(node->right != NULL){
41                 queue.push_back(node->right);
42             }
43         }
44     }
时间: 2024-10-17 19:39:29

[LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路的相关文章

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

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

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