首页 > 其他 > 详细

Populating Next Right Pointers in Each Node

时间:2015-07-09 12:44:05      阅读:258      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

 1 /**
 2  * Definition for binary tree with next pointer.
 3  * struct TreeLinkNode {
 4  *  int val;
 5  *  TreeLinkNode *left, *right, *next;
 6  *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void connect(TreeLinkNode *root) {
12         queue<TreeLinkNode *> q;
13         if(root==NULL)
14             return;
15         q.push(root);
16         while(!q.empty())
17         {
18             TreeLinkNode * tail=NULL;
19             queue<TreeLinkNode *> q2;
20             while(!q.empty())
21             {
22                 TreeLinkNode * temp=q.front();
23                 q.pop();
24                 if(temp->left!=NULL)
25                     q2.push(temp->left);
26                 if(temp->right!=NULL)
27                     q2.push(temp->right);
28                 if(tail!=NULL)
29                 {
30                     tail->next=temp;
31                 }
32                 tail=temp;
33             }
34             q=q2;
35         }
36     }
37 };

 

Populating Next Right Pointers in Each Node

原文:http://www.cnblogs.com/aguai1992/p/4632626.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!