Populating Next Right Pointers in Each Node II
来源:互联网 发布:import form js 编辑:程序博客网 时间:2024/06/18 09:41
Problem:
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
Solution1是自己捣鼓出来的代码,Solution2是讨论区里被赞为elegant的代码。
Solution1:
/**
* 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) {
TreeLinkNode head,p,q;
head = root;
while(head!=null)
{
p = head;
while(p!=null&&p.left==null&&p.right==null)
p = p.next;
if(p==null)
break;
if(p.left!=null)
{
head = p.left;
if(p.right!=null)
{
p.left.next = p.right;
q = p.right;
}
else
q = p.left;
}
else
{
head = p.right;
q = p.right;
}
p=p.next;
while(true)
{
while(p!=null&&p.left==null&&p.right==null)
p = p.next;
if(p==null)
break;
if(p.left!=null)
{
q.next = p.left;
if(p.right!=null)
{
p.left.next = p.right;
q = p.right;
}
else
{
q = p.left;
}
}
else
{
q.next = p.right;
q = p.right;
}
p = p.next;
}
}
}
* 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) {
TreeLinkNode head,p,q;
head = root;
while(head!=null)
{
p = head;
while(p!=null&&p.left==null&&p.right==null)
p = p.next;
if(p==null)
break;
if(p.left!=null)
{
head = p.left;
if(p.right!=null)
{
p.left.next = p.right;
q = p.right;
}
else
q = p.left;
}
else
{
head = p.right;
q = p.right;
}
p=p.next;
while(true)
{
while(p!=null&&p.left==null&&p.right==null)
p = p.next;
if(p==null)
break;
if(p.left!=null)
{
q.next = p.left;
if(p.right!=null)
{
p.left.next = p.right;
q = p.right;
}
else
{
q = p.left;
}
}
else
{
q.next = p.right;
q = p.right;
}
p = p.next;
}
}
}
}
Solution2:
public class Solution {
//based on level order traversal
public void connect(TreeLinkNode root) {
TreeLinkNode head = null; //head of the next level
TreeLinkNode prev = null; //the leading node on the next level
TreeLinkNode cur = root; //current node of current level
while (cur != null) {
while (cur != null) { //iterate on the current level
//left child
if (cur.left != null) {
if (prev != null) {
prev.next = cur.left;
} else {
head = cur.left;
}
prev = cur.left;
}
//right child
if (cur.right != null) {
if (prev != null) {
prev.next = cur.right;
} else {
head = cur.right;
}
prev = cur.right;
}
//move to next node
cur = cur.next;
}
//move to next level
cur = head;
head = null;
prev = null;
}
}
}
//based on level order traversal
public void connect(TreeLinkNode root) {
TreeLinkNode head = null; //head of the next level
TreeLinkNode prev = null; //the leading node on the next level
TreeLinkNode cur = root; //current node of current level
while (cur != null) {
while (cur != null) { //iterate on the current level
//left child
if (cur.left != null) {
if (prev != null) {
prev.next = cur.left;
} else {
head = cur.left;
}
prev = cur.left;
}
//right child
if (cur.right != null) {
if (prev != null) {
prev.next = cur.right;
} else {
head = cur.right;
}
prev = cur.right;
}
//move to next node
cur = cur.next;
}
//move to next level
cur = head;
head = null;
prev = null;
}
}
}
0 0
- Populating Next Right Pointers in Each Node、Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node (I & II)
- Populating Next Right Pointers in Each Node II
- linux常见权限相关错误及解决
- Pascal's Triangle II
- hdu3072——Intelligence System
- Populating Next Right Pointers in Each Node
- 2014年9月15日 停车费应该上涨吗
- Populating Next Right Pointers in Each Node II
- Distinct Subsequences
- Flatten Binary Tree to Linked List
- 常用 索引 约束 查询--格式不变(插入代码-->SQL)
- Jlink无法连接电脑的解决办法
- UNP卷1:第十三章(守护进程和inetd超级服务器)
- Path Sum II
- hackxor渗透平台环境的配置与使用
- Minimum Depth of Binary Tree