码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

时间:2016-02-27 13:42:46      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

题目:Populating Next Right Pointers in Each Node

<span style="font-size:18px;">/*
 * LeetCode Populating Next Right Pointers in Each Node
 * 题目:为树的每一个节点添加一个next指针。指向树状结构排列时自己的右边节点,假设右边没有节点则置为null
 *  * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
package javaTrain;

public class Train10 {
	public void connect(TreeLinkNode root) {
		if(root == null) return;
        root.next = null;
        connectHelp(root.left,root.right);
    }
	private void connectHelp(TreeLinkNode left,TreeLinkNode right){
		if(left == null && right == null) return;
		if(right == null) left.next = null;
		else left.next = right;
		connectHelp(left.left,left.right);
		connectHelp(left.right,right.left);
		connectHelp(right.left,right.right); 
	}
}
</span>


【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

标签:

原文地址:http://www.cnblogs.com/lcchuguo/p/5222774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!