Java实现填充每个节点的下一个右侧节点指针,针对完美二叉树而言

24 篇文章 0 订阅
23 篇文章 0 订阅

标题:Java实现填充每个节点的下一个右侧节点指针,针对完美二叉树而言

做完后,可以看看这个:
Java实现填充每个节点的下一个右侧节点指针,针对不是完美二叉树而言
一、题解

方法一:这个题目的题解,首先会想到用广度遍历实现(非递归版本的),不过这个把每一行二叉树的节点当作链表,更有想法,同时也更优

/**
	 * 执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:38.5 MB, 在所有 Java 提交中击败了88.17% 的用户
	 * @param head
	 * @return
	 */
	public Node hasNext(Node head) {
		if(head == null) {
			return null;
		}
		Node ap = head;
		while(ap != null) {//纵向遍历
			Node bp = ap;
			while(bp != null) {//横向遍历
				if(bp.left != null && bp.right != null) {
					bp.left.next = bp.right;
				}else {
					break;//因为是完美二叉树
				}
				if(bp.next != null && bp.next.left != null) {
					bp.right.next = bp.next.left;
					bp = bp.next;
				}else {
					break;
				}
			}
			ap = ap.left;
		}
		
		return head;
	}

方法二:使用递归实现,这个递归有点东西,小细节

/**
	 * 使用递归
	 * 执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:38.5 MB, 在所有 Java 提交中击败了87.71% 的用户
	 */
	public void hasNext02(Node head) {
		if(head == null) {
			return ;
		}else {
			if(head.left != null && head.right != null) {
				head.left.next = head.right;
			}
			this.hasNext02(head.left);
			if(head.right == null) {
				return ;
			}
			if(head.next != null && head.next.left != null) {
				head.right.next = head.next.left;
			}
			this.hasNext02(head.right);
			
		}
		
	}

完整代码如下:

/**
 * 是否有next [完美二叉树]
 * @author dell
 *
 */
public class TestHasNextPerfect {
	
	/**
	 * 执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:38.5 MB, 在所有 Java 提交中击败了88.17% 的用户
	 * @param head
	 * @return
	 */
	public Node hasNext(Node head) {
		if(head == null) {
			return null;
		}
		Node ap = head;
		while(ap != null) {//纵向遍历
			Node bp = ap;
			while(bp != null) {//横向遍历
				if(bp.left != null && bp.right != null) {
					bp.left.next = bp.right;
				}else {
					break;//因为是完美二叉树
				}
				if(bp.next != null && bp.next.left != null) {
					bp.right.next = bp.next.left;
					bp = bp.next;
				}else {
					break;
				}
			}
			ap = ap.left;
		}
		
		return head;
	}
	
	/**
	 * 使用递归
	 * 执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:38.5 MB, 在所有 Java 提交中击败了87.71% 的用户
	 */
	public void hasNext02(Node head) {
		if(head == null) {
			return ;
		}else {
			if(head.left != null && head.right != null) {
				head.left.next = head.right;
			}
			this.hasNext02(head.left);
			if(head.right == null) {
				return ;
			}
			if(head.next != null && head.next.left != null) {
				head.right.next = head.next.left;
			}
			this.hasNext02(head.right);
			
		}
		
	}
	
	/**
	 * 初始化一个tree
	 * 类广度遍历
	 * @param a
	 * @return
	 */
	public Node initTree(Integer[] a) {
		if(a == null || a.length == 0) {
			return null;
		}
		
		int t = 0;
		Node p = new Node(a[t]);  //至少有一个元素
		Queue<Node> q = new LinkedList<>();
		q.offer(p);
		
		while(!q.isEmpty()) {
			Node node = q.poll();
			if(t + 1 == a.length) {  //先判断数组中是否还有下一个元素
				return p;
			}else {
				t++;
				if(a[t] == null) {  //若下一个元素为null,则不需要创建新的节点
					node.left = null;
				}else {
					node.left = new Node(a[t]);
					q.offer(node.left);
				}
			}
			if(t + 1 == a.length) {
				return p;
			}else {
				t++;
				if(a[t] != null){  //上面的简写,a[t] == null,不需要再赋值
					node.right = new Node(a[t]);
					q.offer(node.right);
				}
			}
		}
		
		return p;
	}
	
	
	@Test
	public void test() {
		Integer[] arr = new Integer[] {1, 2, 3, 4, 5, 9, 10, null, 6, 7, 8, null, null, null, 11};
		Node head = this.initTree(arr);
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值