leetcode[116]:Populating Next Right Pointers in Each Node
来源:互联网 发布:欠淘宝消保金会 编辑:程序博客网 时间:2024/09/21 06:36
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:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \4->5->6->7 -> NULL
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * struct TreeLinkNode *left, *right, *next; * }; * */void connect(struct TreeLinkNode *root) { int i; if(!root) return; if(root->left) { root->left->next=root->right; if(root->next) root->right->next=root->next->left; connect(root->left); connect(root->right); }}
右节点的next就是根节点next(if it has)的左节点。
0 0
- LeetCode 116: Populating Next Right Pointers in Each Node
- LeetCode 116 Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node [116]
- LeetCode(116) Populating Next Right Pointers in Each Node
- [leetcode 116] Populating Next Right Pointers in Each Node
- leetcode || 116、Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node - LeetCode 116
- LeetCode 116 Populating Next Right Pointers in Each Node
- leetcode-116-Populating Next Right Pointers in Each Node
- [Leetcode 116, Medium] Populating Next Right Pointers in Each Node
- leetcode[116]:Populating Next Right Pointers in Each Node
- LeetCode(116) Populating Next Right Pointers in Each Node
- Leetcode #116 Populating Next Right Pointers in Each Node
- [leetcode-116]Populating Next Right Pointers in Each Node(c++)
- [leetcode-116]Populating Next Right Pointers in Each Node(c++)
- leetcode 116: Populating Next Right Pointers in Each Node
- LeetCode(116) Populating Next Right Pointers in Each Node
- LeetCode 116: Populating Next Right Pointers in Each Node
- BZOJ1068
- The Combinatorica Project 组合数学
- leetcode之路006 ZigZag Conversion
- JS 解析XML文件
- Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)
- leetcode[116]:Populating Next Right Pointers in Each Node
- JAVA基础之理解JNI原理
- 2.2 内核VMX基本数据结构与操作
- hdu 4704 Sum||hdu 4869 Turn the pokers 费马小定理
- 关于 使用post 方法出现headers already sent问题
- Android 双卡双待支持检验SIM信息获取
- 【峰回路转】Excel技巧百例 05.数值的阶梯性计算
- 利用神经网络 遗传算法求得函数极小极大值
- 【BLE】CC2541与CC2540的区别