leetcode
来源:互联网 发布:单片机频率计 编辑:程序博客网 时间:2024/06/07 15:55
题目描述
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
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;queue<TreeLinkNode* > queue;TreeLinkNode* tail = root;TreeLinkNode* temp = nullptr;queue.push(root);while (!queue.empty()){temp = queue.front();queue.pop();if (temp->left){queue.push(temp->left);}if (temp->right){queue.push(temp->right);}if (tail == temp){temp->next = nullptr;tail = queue.back();}else{temp->next = queue.front();}}}};
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- Android之批量加载图片OOM问题解决方案
- 按键精灵 使用教程
- 599. Minimum Index Sum of Two Lists
- GalaxyOJ-673 (dfs)
- 润乾报表js跨域调用报表
- leetcode
- python 获取微信好友列表(微信web)
- 同余的一些知识
- 字符串最后一个单词的长度
- 取两个数据结构相同的datatable的差集
- javascript Object.keys()方法
- solr-1 schemal.xml
- 第三章 排序检索数据
- 集成融云即时通讯第一弹