Leetcode - Tree- Populating Next Right Pointers in Each Node
来源:互联网 发布:手机助手网络共享 编辑:程序博客网 时间:2024/06/16 16:52
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:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->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) { root->next=NULL; while(root->left!=NULL) { TreeLinkNode *leftNode=root->left; while(root->next!=NULL) { root->left->next=root->right; root->right->next=root->next->left; root=root->next; } root->left->next=root->right; root=leftNode; } } }};
0 0
- Leetcode - Tree- Populating Next Right Pointers in Each Node
- LeetCode[Tree]: Populating Next Right Pointers in Each Node
- LeetCode[Tree]: Populating Next Right Pointers in Each Node II
- LeetCode[Tree]: Populating Next Right Pointers in Each Node II
- [leetcode][tree][dfs] Populating Next Right Pointers in Each Node
- [leetcode][tree] 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 &&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
- Tree-----116. Populating Next Right Pointers in Each Node 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
- 开心的一天
- 面对任务,我们应该怎么做?
- C++著名程序库的比较和学习经验
- hdu 1026 Ignatius and the Princess I(bfs)
- Driect2D不支持YUV,必须转换成RGB32才能显示
- Leetcode - Tree- Populating Next Right Pointers in Each Node
- 操作系统学习:栈空间和堆空间
- Linux文件相关操作
- memory ordering
- 拷贝增量文件
- Python 最好用的第三方 http 库-Requests介绍
- CSS2.1 特殊性、继承、层叠规则
- Tomcat 数据源配置
- 关于MyEclipse8.5 错误 background indexer crash recovery java.lang.stackoverflowerror