Populating Next Right Pointers in Each Node I&II
2014-03-09 14:15
453 查看
class Solution { public: void connect(TreeLinkNode *root) { if(!root)return ; if(root->left) root->left->next=root->right; if(root->right) root->right->next=(root->next)?root->next->left:NULL; connect(root->left); connect(root->right); } };
做2的时候想仿照1时候的做法,但是因为需要把上一层连接完后,才能连接下一层,所以写了几十行废话。参照了这个:
http://www.cnblogs.com/remlostime/archive/2012/11/25/2787857.html
class Solution { public: void connect(TreeLinkNode *root) { if(root==NULL) return ; TreeLinkNode *p,*q,*nextnode; p=root; q=nextnode=NULL; while(p) { if(p->left) { if(q) q->next=p->left; if(nextnode==NULL) nextnode=p->left; q=p->left; } if(p->right) { if(q) q->next=p->right; if(nextnode==NULL) nextnode=p->right; q=p->right; } p=p->next; } connect(nextnode); } };
相关文章推荐
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
- [LeetCode]Populating Next Right Pointers in Each Node I & II
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
- LeetCode:Populating Next Right Pointers in Each Node I&II
- leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)
- 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(middle)
- 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
- Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看
- leetcode - Populating Next Right Pointers in Each Node I&II
- LeetCode.116(117) Populating Next Right Pointers in Each Node && II
- 【LeetCode】Populating Next Right Pointers in Each Node I & II
- 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