116. 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.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
int a=1;
int b=a;
List<TreeLinkNode> list=LevelTraverse(root); try{
int i=0;
while(i<list.size())
{
TreeLinkNode node=list.get(i);
i++;
b=b-1;
while(b>0)
{
node.next=list.get(i);
i++;
node=node.next;
b--;
}
node.next=null;
b=a*2;
a=b;
}
}catch(NullPointerException e){} }
public ArrayList<TreeLinkNode> LevelTraverse(TreeLinkNode root)//树的水平遍历
{
ArrayList<TreeLinkNode> list=new ArrayList<TreeLinkNode>();
Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
queue.add(root);
try{
while(queue.size()>0)
{
TreeLinkNode a=(TreeLinkNode)queue.peek();
queue.remove();
list.add(a);
if(a.left!=null)
queue.add(a.left);
if(a.right!=null)
queue.add(a.right);
}
}catch(NullPointerException e){} return list;
}
}
116. Populating Next Right Pointers in Each Node的更多相关文章
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- LeetCode OJ 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node
题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...
- leetcode 116 Populating Next Right Pointers in Each Node ----- java
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【LeetCode】116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- 【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
随机推荐
- HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)
我写了个http下载函数,下载txt.jpg都正常,就是下载php有问题:valgrind会报错Conditional jump or move depends on uninitialised va ...
- Power string(poj 2406)
题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len ...
- 设置AFNetworking网络请求的超时时间
http://www.mamicode.com/info-detail-511816.html // 设置超时时间 [manager.requestSerializer willChangeValue ...
- 工具&符号
持续更新中...... 1.RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RP ...
- JDBC 元数据 事务处理
使用 JDBC 驱动程序处理元数据 Java 通过JDBC获得连接以后,得到一个Connection 对象,可以从这个对象获得有关数据库管理系统的各种信息,包括数据库中的各个表,表中的各个列,数据类型 ...
- 数据结构-AVL树的旋转
http://blog.csdn.net/GabrieL1026/article/details/6311339 平衡二叉树在进行插入操作的时候可能出现不平衡的情况,AVL树即是一种自平衡的二叉树,它 ...
- javaNIO是什么?由那几部分组成?各部分的作用。
Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Sel ...
- Cisco IOS debug command reference Command A through D
debug aaa accounting through debug auto-config debug aaa accounting : to display information on acco ...
- android asyncTask 详解
只看http://www.cnblogs.com/xiaoluo501395377/p/3430542.html 足以
- C++学习之类的构造函数、析构函数
在C++的类中,都会有一个或多个构造函数.一个析构函数.一个赋值运算操作符.即使我们自己定义的类中,没有显示定义它们,编译器也会声明一个默认构造函数.一个析构函数和一个赋值运算操作符.例如: //声明 ...