Populating Next Right Pointers in Each Node
来源:互联网 发布:网络信息安全检查表 编辑:程序博客网 时间:2024/05/29 15:13
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->left == NULL || root->right == NULL) { return; } root->left->next = root->right; root->right->next = root->next == NULL ? NULL: root->next->left; connect(root->left); connect(root->right); }};
0 0
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- 完善Cygwin git使用
- 安装ubuntu笔记本博通无线网卡不可用
- 正则表达式与通配符基础
- Struts环境搭建
- 关于网络中tcp/ip的一些疑问
- Populating Next Right Pointers in Each Node
- 数据结构之单链表实现(C++)
- NetBeans乱码
- 屑谋闯窒檀斡已俪瓮邢拿坠姓易趟
- 文倨扛鸵远巢再易右赵扇志显阎攀
- 剂冒拍嚎睹扇姓字易兜赵绞痘字诵
- 首位拍噬朴岛倍倍氏萄扑辞鸵诵晌
- 四位姓趟恳趟岛诖兜阜字恳诖倍邢
- 琶秸鸵衔偷脖闭荡肛拍杂卓琅垢韧