LeetCode:Populating Next Right Pointers in Each Node I,II 编程之美3.10
来源:互联网 发布:c语言求绝对值 编辑:程序博客网 时间:2024/05/08 05:38
解答:
队列实现按排遍历
class Solution {public:struct InvoLevelNode { TreeLinkNode *node; int level; InvoLevelNode(TreeLinkNode *p,int n):node(p),level(n){} InvoLevelNode(){}//重定义构造函数时,一定要把程序里面出现情况全部构造完全}; void connect(TreeLinkNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root ==NULL)return ; deque<InvoLevelNode>L; int level = 1; InvoLevelNode p,q; L.push_back(InvoLevelNode(root,1)); while(L.size()) { p = L.front(); L.pop_front(); if(p.node->left) L.push_back(InvoLevelNode(p.node->left,p.level+1)); if(p.node->right) L.push_back(InvoLevelNode(p.node->right,p.level+1)); if(L.size()) q= L.front(); else { p.node->next = NULL; continue; } if(p.level == q.level) p.node->next = q.node; else p.node->next = NULL; } }};
查看编程之美 3.10 分层遍历二叉树感觉代码更好点 如下
class Solution {public: void connect(TreeLinkNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root==NULL)return; vector<TreeLinkNode*> V; TreeLinkNode* node; int last = 1; int nowp = 0; V.push_back(root); while(last!= nowp) { node = V[nowp]; if(node->left) //这个要在last更新之前做 V.push_back(node->left); if(node->right) V.push_back(node->right); if(nowp == last - 1 ) { node->next = NULL; last = V.size();//遍历完一层,新压入的只有可能是下一层的元素 } else { node->next = V[nowp+1]; } nowp++; } }};
- LeetCode:Populating Next Right Pointers in Each Node I,II 编程之美3.10
- 【LeetCode】Populating Next Right Pointers in Each Node I & II
- LeetCode:Populating Next Right Pointers in Each Node I & II
- [LeetCode] Populating Next Right Pointers in Each Node I &II
- LeetCode:Populating Next Right Pointers in Each Node I&II
- [Leetcode]Populating Next Right Pointers in Each Node I && II
- LeetCode - Populating Next Right Pointers in Each Node I && II
- 【leetcode】Populating Next Right Pointers in Each Node I&&II
- [LeetCode] Populating Next Right Pointers in Each Node I, II
- [LeetCode]Populating Next Right Pointers in Each Node I & 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】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 I
- LeetCode 之 Populating Next Right Pointers in Each Node I II — C++ 实现
- Populating Next Right Pointers in Each Node (I & II)
- Populating Next Right Pointers in Each Node I&II
- wifi的相关信息
- GDI绘图方法的初步分析
- linux下配置host
- java程序连接TimesTen
- ORACLE中的字符串替换 replce、regexp_replace 和 translate
- LeetCode:Populating Next Right Pointers in Each Node I,II 编程之美3.10
- centos下查看磁盘的读写状况
- 关于“广州市劳动保障网上业务大厅"登陆不支持chrome浏览器
- winform 下 Treeview中CheckBox选中项的技巧
- C++中string的用法
- linux vi的命令操作
- 系统限制:sysconf
- pthread_cond_init
- 由自身经历谈“不谋全局者,不足以谋一域”