首页 > 其他 > 详细

Leetcode#117 Populating Next Right Pointers in Each Node II

时间:2015-01-21 14:55:28      阅读:253      评论:0      收藏:0      [点我收藏+]

原题地址

 

二叉树的层次遍历。

对于每一层,依次把各节点连起来即可。

 

代码:

 1 void connect(TreeLinkNode *root) {
 2   if (!root) return;
 3 
 4   queue<TreeLinkNode *> parents;
 5 
 6   parents.push(root);
 7   while (!parents.empty()) {
 8     queue<TreeLinkNode *> children;
 9 
10     while (!parents.empty()) {
11       TreeLinkNode *p = parents.front();
12       parents.pop();
13       p->next = parents.empty() ? NULL : parents.front();
14       if (p->left)
15         children.push(p->left);
16       if (p->right)
17         children.push(p->right);
18     }
19     parents = children;
20   }
21 }

 

Leetcode#117 Populating Next Right Pointers in Each Node II

原文:http://www.cnblogs.com/boring09/p/4238730.html

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