Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode

http://blog.csdn.net/fightforyourdream/article/details/14514165

在上题基础上扩展,此时数不是完美二叉树,而是任意二叉树

这道题有些tricky,有时间回头再研究一下


package Level4;

import Utility.TreeLinkNode;

/**
 * Populating Next Right Pointers in Each Node II 
 * 
 *  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
Discuss


 *
 */
public class S117 {

	public static void main(String[] args) {

	}

	public static void connect(TreeLinkNode root) {
		// 空节点就直接返回
		if (root == null){
			return;
		}
		
		// 找到与root同一行的next node
		TreeLinkNode rootNext = root.next;
		TreeLinkNode next = null;		// 下一个被连接的对象
		
		// rootNext如果是null说明已经处理完这一层的所有node
		// next不等于null说明找到了找到最左边的下一个被连接的对象
		while (rootNext != null && next == null)
		{
			if (rootNext.left != null){	// 优先找左边
				next = rootNext.left;
			} else{
				next = rootNext.right;
			}
			rootNext = rootNext.next;
		}
 
		if (root.left != null)
		{
			if (root.right != null){	//	内部相连
				root.left.next = root.right;
			}else{						// 跨树相连
				root.left.next = next;
			}
		}
		if (root.right != null){		// 跨树相连
			root.right.next = next;
		}
		
		connect(root.right);		// 要先让右边都先连起来
		connect(root.left);
    }
}


第二次做修改了一个地方的bug(在找rootnext时增加了break,及时退出),居然提交两次就通过了。

果然做LeetCode的真谛在于:遇到不懂 => 查答案 => 自己理解写出来或背下来 => 过一段时间后第二遍重做

比如这道题,我印象最深刻的是一定要先处理右子树再处理左子树,知道这点就足够写出来了。

关键就是找到next节点是在哪里,而这又要求找到rootnext节点在哪里。那个while循环就是破题的关键。


/**
 * 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) {
        if(root == null){
            return;
        }
        
        TreeLinkNode rootnext = root.next;
        TreeLinkNode next = null;
        while(rootnext != null){
            if(rootnext.left != null){
                next = rootnext.left;
                break;
            }else if(rootnext.right != null){
                next = rootnext.right;
                break;
            }else{
                rootnext = rootnext.next;
            }
        }
        
        if(root.right != null){
            root.right.next = next;
        }
        if(root.left != null){
            if(root.right != null){
                root.left.next = root.right;
            }else{
                root.left.next = next;
            }
        }
        
        connect(root.right);
        connect(root.left);
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值