Populating Next Right Pointers in Each Node II
来源:互联网 发布:linux启动tomcat日志 编辑:程序博客网 时间:2024/04/29 16:28
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
/** * 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; outside(root->left,root->right); connect(root->left); connect(root->right); } void outside(TreeLinkNode* l,TreeLinkNode* r){ if(l==NULL||r==NULL) return; else l->next=r; outside(l->left,r->right); outside(l->left,r->left); outside(l->right,r->right); outside(l->right,r->left); }};
0 0
- Populating Next Right Pointers in Each Node、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
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: 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
- 【leetcode】Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node (I & II)
- Populating Next Right Pointers in Each Node II
- yo generator 对象 方法
- 为dialog添加items,并且控制每个activity只能出现一个dialog,不会重复弹出
- Android动画学习笔记-Android Animation
- Spark修炼之道(基础篇)——Linux大数据开发基础:第七节:进程管理
- NOIP2015提高组 总结
- Populating Next Right Pointers in Each Node II
- 自定义圆形头像图标控件
- 那些老牌互联网公司现在都混得怎样了?
- 使用Dom4j解析XML
- Spark修炼之道(基础篇)——Linux大数据开发基础:第八节:网络管理
- HDOJ1868Oulipo(KMP算法)
- hive配置mysql元数据
- 4412裸机程序之UART串口
- 谈谈spring中bean的名字