【second】Populating Next Right Pointers in Each Node
2013-10-23 14:47
246 查看
void connect(TreeLinkNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. if(!root) return; TreeLinkNode* first,*cur; first = cur = root; while(cur) { if(cur->left) { cur->left->next = cur->right; if(cur->next) cur->right->next = cur->next->left; }else return; cur = cur->next; if(cur==NULL) { cur = first->left; first = cur; } } }
其实本质上是tree的level-order遍历
void connect(TreeLinkNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. if(!root) return; TreeLinkNode* first,*cur; first = root; while(first) { cur = first; while(cur) { if(cur->left) { cur->left->next = cur->right; if(cur->next) cur->right->next = cur->next->left; }else return; cur = cur->next; } first = first->left; } }
相关文章推荐
- 【second】Populating Next Right Pointers in Each Node II
- [leetcode]Populating Next Right Pointers in Each Node II
- [leetcode-116]Populating Next Right Pointers in Each Node(c++)
- 【leetcode】116. Populating Next Right Pointers in Each Node
- 【LeetCode】Populating Next Right Pointers in Each Node I & II
- Leetcode | Populating Next Right Pointers in Each Node I & II
- 117. Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node
- leetcode Populating Next Right Pointers in Each Node &&Populating Next Right Pointers in Each Node||
- LeetCode OJ 116. Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node--为每一个节点填充next right指针
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看
- Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II (Java)
- 【Leetcode】:116. Populating Next Right Pointers in Each Node 问题 in JAVA
- leetcode_116题——Populating Next Right Pointers in Each Node(树,广度优先搜索)
- [LeetCode] Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node