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;
}
}
}