116. Populating Next Right Pointers in Each Node
来源:互联网 发布:ubuntu命令行升级系统 编辑:程序博客网 时间:2024/06/06 15:44
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
这题挺好的 因为有了next指针 所以现在可以直接层次遍历了
public void connect(TreeLinkNode root) { TreeLinkNode level_start=root; while(level_start!=null){ TreeLinkNode cur=level_start; while(cur!=null){ if(cur.left!=null) cur.left.next=cur.right; if(cur.right!=null && cur.next!=null) cur.right.next=cur.next.left; cur=cur.next; } level_start=level_start.left; }}
阅读全文
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
- ubuntu 16.04源
- 从整数数组中找出唯二奇数次的数字 Python版
- 单链表 c实现 详细注释
- 常用Linux命令
- npm包管理工具的安装及配置使用
- 116. Populating Next Right Pointers in Each Node
- 牛客网 1222 01
- 15算法课程 303. Range Sum Query
- if条件语句 加括号与不加 括号 的区别
- 如何改变Redis用不好的误区
- 24. Swap Nodes in Pairs
- 2.四个核心概念及使用
- POI+JFreeChart生成报表图片在Excel中的位置
- 构造一个特殊栈,实现方法getmin,返回栈中的最小元素 Python版