LeetCode Populating Next Right Pointers in Each Node II
来源:互联网 发布:编程算法基础pdf 编辑:程序博客网 时间:2024/04/28 01:09
Populating Next Right Pointers in Each Node II
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: TreeLinkNode* get_next(TreeLinkNode* root){ if(!root) return NULL; if(root->left) return root->left; if(root->right) return root->right; return get_next(root->next); } void connect(TreeLinkNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function TreeLinkNode* left=root; while(left){ root=left; while(root){ if(root->left&&root->right){ root->left->next=root->right; root->right->next=get_next(root->next); } else if(root->left||root->right) get_next(root)->next=get_next(root->next); root=root->next; } left=get_next(left); } }};
- 【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
- 收入是由什么决定的
- java-为指定的图像文件加透明阴影
- 【Practice】白名单过滤程序
- 收集的一些电子书
- 游走Android系列之网络通信
- LeetCode Populating Next Right Pointers in Each Node II
- 4.在二元树中找出和为某一值的所有路径
- 几米笔下的十二星座
- 盘点《爱情公寓3》里的30句经典爆笑台词
- install-info error during update
- LeetCode Pow(x,n)
- ajax执行成功返回error回调函数与jquery.sirialize()乱码问题
- CentOS5 yum相关操作
- J2EE的安全认证机制