Populating Next Right Pointers in Each Node Python Java Leetcode
2016-06-20 08:06
489 查看
Given a binary tree
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:
JAVA:
(easy)
public void connect(TreeLinkNode root) {
if(root == null)
return;
if(root.left != null){
root.left.next = root.right;
if(root.next != null)
root.right.next = root.next.left;
}
connect(root.left);
connect(root.right);
}
(hard)
python:
#coding:utf-8
class TreeLinkNode:
def __init__(self,val,left,right,next1):
self.val=val
self.left=left
self.right=right
self.next1=next1
class Solution:
def resolve(self,root):
if not root:
return None
cur=root
next=root.left
while cur.left is not None:
cur.left.next=cur.right
if cur.next is not None:
cur.right.next=cur.next.left
cur=cur.next
else:
cur=next
next=cur.left
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 / 4000 \ / \ 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ / \ 4->5->6->7 -> NULL
JAVA:
(easy)
public void connect(TreeLinkNode root) {
if(root == null)
return;
if(root.left != null){
root.left.next = root.right;
if(root.next != null)
root.right.next = root.next.left;
}
connect(root.left);
connect(root.right);
}
(hard)
java: class TreeNode{ int val; TreeNode left=null; TreeNode right=null; void TreeNode(int val){ this.val=val; } } class TreeLinkNode{ int val; TreeLinkNode left=null; TreeLinkNode right=null; TreeLinkNode next=null; void TreeLinkNode(int val){ this.val=val; } } public class Solution{ public TreeLinkList reslove(TreeNode root){ List<List<TreeNode>> list=new ArrayList<List<TreeNode>>(); Queue<TreeNode> queue=new LinkedList<TreeNode>(); if(root==null) return null; queue.offer(root); while(!queue.isEmpty){ List<TreeNode> subList=new ArrayList<TreeNode>(); int queuesize=queue.size(); for(int i=0;i<queuesize;i++){ if(queue[i].left!=null){ queue.offer(queue.peer().left); } if(queue[i].right!=null){ queue.offer(queue.peer().right); } subList.add(queue.poll()); } list.add(subList); } for(int i=0;i<list.length,i++){ if(list[i].length>1){ for(int j=1;j<list[i].length;j++){ list[j-1].next=list[j]; } list[list[i].length-1].next=null; } else{ list[i].next=null; } } return list[0][0]; } }
python:
#coding:utf-8
class TreeLinkNode:
def __init__(self,val,left,right,next1):
self.val=val
self.left=left
self.right=right
self.next1=next1
class Solution:
def resolve(self,root):
if not root:
return None
cur=root
next=root.left
while cur.left is not None:
cur.left.next=cur.right
if cur.next is not None:
cur.right.next=cur.next.left
cur=cur.next
else:
cur=next
next=cur.left
相关文章推荐
- ubuntu上面安装nodejs,npm,bower,grunt,yeoman
- nodejs ajax跨域
- 路径匹配模块-glob
- NodeJS、NPM安装配置步骤(windows版本)
- Populating Next Right Pointers in Each Node
- Count Complete Tree Nodes
- [freeCodeCamp] Start a Nodejs Server - Complete "Make it Modular"
- Nodejs与ES6系列1:变量声明
- Nodejs与ES6系列2:Promise对象
- Nodejs与ES6系列3:generator对象
- Nodejs与ES6系列4:ES6中的类
- Namenode HA自动切换 从Namenode报错Got a command from standby NN - ignoring command:2
- TypeError: elem.nodeName is undefined
- nodeJS中的事件--event
- NodeJS中的异步I/O、事件驱动
- Node.js环境中同步分行读取文件
- nodemailer 发邮件
- Node.js:ubuntu安装Node.js
- 使用NodeJs创建一个微型服务器
- 使用NodeJs的express模块创建一个服务器