[LeetCode] 117. Populating Next Right Pointers in Each Node II
来源:互联网 发布:算法竞赛谜题 编辑:程序博客网 时间:2024/06/05 02:21
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 7After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
class Solution {public: void connect(TreeLinkNode *root) { vector<TreeLinkNode *> nxts; connecthelper(root, nxts, 0); }private: void connecthelper(TreeLinkNode *root, vector<TreeLinkNode *> &nxts, int depth) { if (root == nullptr) return; if (depth >= nxts.size()) { nxts.push_back(root); //root->next = nullptr; } else { root->next = nxts[depth]; nxts[depth] = root; } connecthelper(root->right, nxts, depth + 1); connecthelper(root->left, nxts, depth + 1); }};
阅读全文
0 0
- 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 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] 117. Populating Next Right Pointers in Each Node II
- LeetCode#117. Populating Next Right Pointers in Each Node II
- 技术团队的一些事情
- SpringMVC源码(零)SpringMVC的架构流程
- Jmeter:物理内存和虚拟内存
- Android插件DSL参考
- vmstat和top命令解释
- [LeetCode] 117. Populating Next Right Pointers in Each Node II
- 【剑指offer】题28:字符串的排列
- sinon.js的spy、stub和mock
- linux下安装warpctc,mxnet和python接口配置
- POJ1017-Packets
- php面试分享(三)
- Error during artifact deployment. See server log for details解决办法
- vmware获取虚拟机所有权的问题
- Photoshop抠图(磁性钢笔工具)