Populating Next Right Pointers in Each Node II
来源:互联网 发布:叁度软件破解版 编辑:程序博客网 时间:2024/06/07 01:15
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
点击打开原题链接
基本上是层次遍历稍微修改下就可以了,算法比较简单,注意下边界条件即可,也可以用把每一层放入vector<vector<> >中去,但是会开辟新的内存,这里
直接判断是否在一层上,代码稍微复杂一点点。
struct TreeLinkNode { int val; TreeLinkNode *left, *right, *next; TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}}; struct node { int level; TreeLinkNode* TLN; };class Solution {public:void connect(TreeLinkNode *root) {if (root == NULL){return;}deque<node> deque_TreeLinkNode;node pre,current;pre.level = 0;pre.TLN = root;deque_TreeLinkNode.push_back(pre);pre.TLN = NULL;while(!deque_TreeLinkNode.empty()){node temp = deque_TreeLinkNode.front();if (temp.TLN->left != NULL){node left;left.level = temp.level+1;left.TLN = temp.TLN->left;deque_TreeLinkNode.push_back(left);}if (temp.TLN->right != NULL){node right;right.level = temp.level+1;right.TLN = temp.TLN->right;deque_TreeLinkNode.push_back(right);}current= deque_TreeLinkNode.front();deque_TreeLinkNode.pop_front();if (pre.TLN == NULL){pre.TLN = current.TLN;pre.level = current.level;//deque_TreeLinkNode.pop_front();continue;}else{if (current.level == pre.level){pre.TLN->next = current.TLN;pre=current;continue;//current->TLN = NULL;//current->level = 0;}else{pre.TLN->next = NULL;pre = current;}}}pre.TLN->next = NULL;}private:};
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
- C++primer第5版课后练习习题答案7.23
- 下载provinces_cities.plist
- hdu 4054 模拟 练习十六进制输出
- 三大框架之Hibernate
- 前缀表达式的求值
- Populating Next Right Pointers in Each Node II
- JQuery实现图片轮换
- Matlab程序转C++/OpenCV不可不知的17个函数
- 计算表达式的值c++逆波兰式实现方法
- Leetcode:unique_binary_search_trees
- Codeforces Bayan Bus
- iOS笔记--多点触碰的时候如何获取其中的某一次点击事件
- linux下fflush(stdin)的使用问题
- PHP搭建(windows64+apache2.4.7+mysql-5.6+php5.5+phpMyAdmin)和Discuz安装