【leetcode】Populating Next Right Pointers in Each Node
来源:互联网 发布:小米6怎么样知乎 编辑:程序博客网 时间:2024/03/29 05:38
Populating Next Right Pointers in Each Node
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1 / \ 2 3 / \ / \ 4 5 6 7
After calling your function, the tree should look like:
注意几个判断条件:
/** * 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; TreeLinkNode *current; while(root->left!= NULL) { current=root; while(current!= NULL) { current->left->next=current->right; if(current->next!= NULL) { current->right->next=current->next->left; } current=current->next; } root=root->left; } }};
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 &&Populating Next Right Pointers in Each Node||
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- 【leetcode】 Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- leetcode 79: Populating Next Right Pointers in Each Node II
- Leetcode: Populating Next Right Pointers in Each Node
- [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
- 从fragment中获取需要的控件
- PHP curl 上传文件
- java中简单的字符压缩实现,aaabbcccd压缩成3a2b3c1d
- deferred对象
- 拆半查找的递归和非递归算法
- 【leetcode】Populating Next Right Pointers in Each Node
- P51 ,12
- iOS新人学习AFNetworking(四)---AFNetworiking与ASIHttpRequest对比
- 作业2.30
- 如何使用UIAutomation进行iOS 自动化测试
- SQL Server各种导入导出数据方式的比较
- uva 11991
- UITableViewCell添加动画
- web.xml中配置spring的几种方式