Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
来源:互联网 发布:网络为什么看不了视频 编辑:程序博客网 时间:2024/06/08 04:52
Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
Tag: Tree, Depth-first Search
Difficulty: Medium
/*116. Populating Next Right Pointers in Each Node (Medium)Given a binary treestruct TreeLinkNode {TreeLinkNode *left;TreeLinkNode *right;TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL.Note:You may only use constant extra space.You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).For example,Given the following perfect binary tree,1/ \2 3/ \ / \4 5 6 7After calling your function, the tree should look like:1 -> NULL/ \2 -> 3 -> NULL/ \ / \4->5->6->7 -> NULL*//*** 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 == NULL) {return;}queue<TreeLinkNode*> q;q.push(root);while (!q.empty()) {int _size = q.size();for (int i = 0; i < _size; i++) {TreeLinkNode* node = q.front();q.pop();if (i < _size - 1) {node->next = q.front();}else {node->next = NULL;}if (node->left != NULL) {q.push(node->left);}if (node->right != NULL) {q.push(node->right);}}}}};
0 0
- Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
- [Leetcode 116, Medium] Populating Next Right Pointers in Each Node
- LeetCode #116 - Populating Next Right Pointers in Each Node - Medium
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]116.Populating Next Right Pointers in Each Node
- [Leetcode] 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116.Populating Next Right Pointers in Each Node
- 116. Populating Next Right Pointers in Each Node LeetCode
- leetcode 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode *** 116. Populating Next Right Pointers in Each Node
- leetcode-116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]problem 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116. Populating Next Right Pointers in Each Node
- 奥巴马最近的精彩演讲《我们为什么要上学》英汉互译
- Leetcode Flatten Binary Tree to Linked List
- java环境变量配置
- Leetcode 137. Single Number II (Medium) (cpp)
- 杂谈 | Hello CSDN
- Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
- TEC1701.WebADI开发技术总结 - 第二章 Step2 - 创建数据库对象(2/6)
- TEC1701.WebADI开发技术总结 - 第一章 Step1 - Web ADI客户化开发设置(1/6)
- ubuntu安装scikit-image失败
- nginx源码学习1 ngx_queue
- InteliJ IDEA使用文档
- TEC1701.WebADI开发技术总结 - 第三章 Step3 - 配置Web ADI(3/6)
- fatal error: QWebView: No such file or directory
- Shadowsocks:从网段1转接到网段2