【populating-next-right-pointers-in-each-node-ii】
来源:互联网 发布:树莓派的编程语言 编辑:程序博客网 时间:2024/06/05 04:56
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
题意:把每一层节点链接起来
思路:层次遍历的思想,将一层的元素全部入队,然后将本层每个节点的子节点一次全部入队
class Solution{public:void connect(TreeLinkNode* root){if (root==NULL){return;}TreeLinkNode* tail = root;TreeLinkNode* tmp;queue<TreeLinkNode*> q;q.push(root);while (q.size()){tmp = q.front();q.pop();if (tmp->left!=NULL){q.push(tmp->left);}if (tmp->right!=NULL){q.push(tmp->right);}if(tmp==tail){tmp->next = NULL;tail = q.back();}else{tmp->next = q.front();}}}};
阅读全文
0 0
- Populating Next Right Pointers in Each Node、Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node (I & II)
- Populating Next Right Pointers in Each Node II
- 实训第三天~
- android studio打开项目后一直Gradle:Resolve dependecies 'app_XXAPk'很长时间?
- uget使用aria2,出现Is aria2 shutdown的错误
- 问题 C: 二哈大定理
- 【风宇冲】Unity3D教程宝典之Shader篇:第四讲制作一个美丽的地球 + 云层颜色的一点见解
- 【populating-next-right-pointers-in-each-node-ii】
- 170907_学习测试(三)_Apache JMeter 入门(二)_JMeter脚本录制
- 2 XML schema
- 函数
- 《C++ Primer》读书笔记第四章-1-基础 And 运算符
- 三大框架之Spring (初级学习 2)
- springMVC整合freemarker的使用
- ajax使用serialize()方法后,数据无法封装并获取(PUT提交方式!)
- Android源码:AMS、PMS、WMS原理分析