(java) Populating Next Right Pointers in Each Node
2016-03-06 20:15
573 查看
Given a binary tree
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
Initially, all next pointers are set to
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,
After calling your function, the tree should look like:
思路:就是一个BFS,把每一层的节点向右链接一下
代码如下(已通过leetcode)
public class Solution {
public void connect(TreeLinkNode root) {
List<TreeLinkNode> list=new ArrayList<TreeLinkNode>();
if(root==null) return;
Queue que=new LinkedList<>();
que.add(root);
BFS(list,que);
List<List<TreeLinkNode>> lists=new ArrayList<List<TreeLinkNode>>();
int count=0;
int i=0;
while(count<list.size()) {
List<TreeLinkNode> temp=list.subList((int)Math.pow(2, i)-1,(int)Math.pow(2, i+1)-1);
lists.add(temp);
count+=(int)Math.pow(2, i);
i++;
}
for(int j=0;j<lists.size();j++) {
int k=0;
while(k<lists.get(j).size()-1) {
lists.get(j).get(k).next=lists.get(j).get(k+1);
k++;
}
}
}
private void BFS(List<TreeLinkNode> list,Queue que) {
// TODO Auto-generated method stub
TreeLinkNode temp;
while(!que.isEmpty()) {
temp=(TreeLinkNode) que.remove();
list.add(temp);
if(temp.left!=null) {
que.add(temp.left);
que.add(temp.right);
}
}
}
}
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
思路:就是一个BFS,把每一层的节点向右链接一下
代码如下(已通过leetcode)
public class Solution {
public void connect(TreeLinkNode root) {
List<TreeLinkNode> list=new ArrayList<TreeLinkNode>();
if(root==null) return;
Queue que=new LinkedList<>();
que.add(root);
BFS(list,que);
List<List<TreeLinkNode>> lists=new ArrayList<List<TreeLinkNode>>();
int count=0;
int i=0;
while(count<list.size()) {
List<TreeLinkNode> temp=list.subList((int)Math.pow(2, i)-1,(int)Math.pow(2, i+1)-1);
lists.add(temp);
count+=(int)Math.pow(2, i);
i++;
}
for(int j=0;j<lists.size();j++) {
int k=0;
while(k<lists.get(j).size()-1) {
lists.get(j).get(k).next=lists.get(j).get(k+1);
k++;
}
}
}
private void BFS(List<TreeLinkNode> list,Queue que) {
// TODO Auto-generated method stub
TreeLinkNode temp;
while(!que.isEmpty()) {
temp=(TreeLinkNode) que.remove();
list.add(temp);
if(temp.left!=null) {
que.add(temp.left);
que.add(temp.right);
}
}
}
}
相关文章推荐
- hello node world
- Windows下配置Node.js
- Node.js简介
- node模块详解
- Node.js模块引入
- Node.js程序中的本地文件操作用法小结
- LeetCode OJ刷题历程——Remove Nth Node From End of List
- 安装node.js,解决ionic安装
- Node.js模块封装及使用方法
- lintcode-easy-Remove Nth Node from End of List
- Node.js模块封装及使用
- Node基础篇(文件操作)
- Node基础篇(模块和NPM)
- Node基础篇(概要)
- Node.js程序中的本地文件操作用法小结
- Node.js模块封装及使用方法
- 让nodejs应用后台执行:forever
- node.js定时事务node-schedule
- LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)
- 一个由INode节点爆满引起的业务故障 推荐