Populating Next Right Pointers in Each Node II
来源:互联网 发布:数控西门子系统编程 编辑:程序博客网 时间:2024/05/16 10:18
<pre name="code" class="cpp">/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} * }; */class Solution {public: void connect(TreeLinkNode *root) { if(!root) return; TreeLinkNode* lastHead = root; TreeLinkNode* pre = NULL; TreeLinkNode* curHead = NULL; while(lastHead) { TreeLinkNode* lastCur = lastHead; while(lastCur) { if(lastCur->left) { if(curHead == NULL) { curHead = lastCur->left; pre = curHead; } else { pre->next = lastCur->left; pre = pre->next; } } if(lastCur->right) { if(curHead == NULL) { curHead = lastCur->right; pre = curHead; } else { pre->next = lastCur->right; pre = pre->next; } } lastCur = lastCur->next; } lastHead = curHead; curHead = NULL; } }};
代码同样适用于Populating Next Right Pointers in Each Node,感谢Code_Ganker
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
- codevs 进制转换 模拟
- java抓取网页代码
- Coarse-to-fine CNN 人脸特征点定位思路
- ubuntu 14.04 ssh
- [ATL/WTL]_[初级]_[SDI程序关闭的注意事项]
- Populating Next Right Pointers in Each Node II
- 05-树6. Path in a Heap
- APM_源码分析
- Ruby On Rails开发所需插件整理
- To The Max(hdu-1081)
- node.js用户注册功能的完善
- 统一认证系统实现要点-资源认证
- 《标记》-编码一次,解码两次?
- PHP字符串分割函数explode,strtok,str_spli的用法