LeetCode(117) Populating Next Right Pointers in Each Node II
来源:互联网 发布:哈尔滨,校园网络建设 编辑:程序博客网 时间:2024/06/16 02:19
题目
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
分析
为一颗给定的二叉树的每个节点添加next节点,next指针指向该节点所在二叉树层的下一个节点。
注意,题目要求空间复杂度为常量。
用两种方法解决该问题:
方法一,借助queue数据结构存储每一层的节点,遍历该层节点逐个添加next指针。该方法空间复杂度是O(n)的,因为每个节点都需要在队列中保存一次。
方法二:不利用额外的空间存储节点,直接操作二叉树,参考链接算法出自网址
AC代码
class Solution {public: //方法一:利用层次遍历的思想 void connect1(TreeLinkNode *root) { if (!root) return; else if (!root->left && !root->right) { root->next = NULL; return; } queue<TreeLinkNode *> qt; qt.push(root); while (!qt.empty()) { queue<TreeLinkNode *> tmp; TreeLinkNode *p = qt.front(); //把 当前节点的 左右子节点压入临时队列 if (p->left) tmp.push(p->left); if (p->right) tmp.push(p->right); qt.pop(); while (!qt.empty()) { TreeLinkNode *q = qt.front(); p->next = q; p = q; //把 当前节点的 左右子节点压入临时队列 if (q->left) tmp.push(q->left); if (q->right) tmp.push(q->right); qt.pop(); } p->next = NULL; qt = tmp; }//while return; } //方法二:直接操作二叉树节点 void connect(TreeLinkNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if (root == NULL) return; TreeLinkNode *p = root; TreeLinkNode *q = NULL; TreeLinkNode *nextNode = NULL; while (p) { if (p->left) { if (q) q->next = p->left; q = p->left; if (nextNode == NULL) nextNode = q; } if (p->right) { if (q) q->next = p->right; q = p->right; if (nextNode == NULL) nextNode = q; } p = p->next; } connect(nextNode); }};
GitHub测试程序源码
0 0
- LeetCode 117 Populating Next Right Pointers in Each Node II
- LeetCode: Populating Next Right Pointers in Each Node II [117]
- LeetCode 117 Populating Next Right Pointers in Each Node II
- [leetcode 117] Populating Next Right Pointers in Each Node II
- [LeetCode 117]Populating Next Right Pointers in Each Node II
- leetcode || 117、Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II - LeetCode 117
- leetcode[117]:Populating Next Right Pointers in Each Node II
- Leetcode #117 Populating Next Right Pointers in Each Node II
- LeetCode 117: Populating Next Right Pointers in Each Node II
- LeetCode 117:Populating Next Right Pointers in Each Node II
- leetcode 117 Populating Next Right Pointers in Each Node II
- Leetcode 117 Populating Next Right Pointers in Each Node II
- LeetCode 117 Populating Next Right Pointers in Each Node II
- Leetcode-117: 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 II
- [Leetcode] Populating Next Right Pointers in Each Node II
- Spark修炼之道(进阶篇)——Spark入门到精通:第三节 Spark Intellij IDEA开发环境搭建
- 5.UIButton(按钮)知识总结
- 负反馈简要说明
- 把一个10进制的数转换成16进制数(java)
- FaceBook通过accesstoken获取用户的信息
- LeetCode(117) Populating Next Right Pointers in Each Node II
- 小认识监听器接口
- MYSQL卸载
- linux spi子系统
- 进程调度算法模拟,用动态优先数及时间片轮转法实现进程调度_Java语言模拟实现
- HDU_4115_Eliminate the Conflict(2-SAT)
- findbugs安装方法
- linux + busybox编译以及在qemu中运行
- 用Darwin和live555实现的直播框架