[leetcode]Populating Next Right Pointers in Each Node II
来源:互联网 发布:aft3登陆器源码 编辑:程序博客网 时间:2024/05/29 15:14
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
记录二叉树中每一层的信息,DFS
/** * 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: map<int,TreeLinkNode *> m; void conn(TreeLinkNode *root,int level){ if(level==1||m.find(level)==m.end()) m.insert(pair<int,TreeLinkNode *>(level, root)); else{ m[level]->next = root; m[level] = root; } if(root->left!=NULL) conn(root->left,level+1); if(root->right!=NULL) conn(root->right,level+1); } void connect(TreeLinkNode *root) { if(root == NULL) return; conn(root,1); }};
0 0
- 【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
- [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 - 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]Populating Next Right Pointers in Each Node II
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- STC15F2K60S2烧写程序时卡在开始调节频率处,下载失败
- LeetCode 017 Letter Combinations of a Phone Number
- IOS微博项目之UIScrollView&UIPageControl
- 教训 : 公用模块修改要注意,能添加的就不要修改
- [leetcode]Populating Next Right Pointers in Each Node II
- UVa 10603 倒水
- iOS/Xcode 自动生成注释
- centos 用户和组,权限,添加删除详解
- 程序员在互联网公司和行业软件公司工作,有什么区别?
- Linux下select, poll和epoll IO模型的详解
- 数据库事务与其隔离级别
- 3.4 同时打开
- Opengl es+win7