判断是否完全二叉树

1.准备节点

	public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}

2.方法1

/**
	 * check whether entirely binary tree
	 * function one
	 * @param head
	 * @return
	 */
	public static boolean isEntirely1(Node head) {
		if(head == null){
			return true;
		}
		// level traversal
		Queue<Node> queue = new LinkedList<>();
		queue.add(head);
		// default is not a leaf node
		boolean leaf = false;
		while (!queue.isEmpty()){
			head = queue.poll();
			/*
			if the node is leaf node
			the node left and right must null
			if the node not is leaf node
			the node left is null , right not null
				==> not entirely binary tree
			 */
			if((leaf && (head.left != null || head.right != null)) || (head.left == null && head.right != null)){
				return false;
			}
			if(head.left != null ){
				queue.add(head.left);
			}
			if(head.right != null ){
				queue.add(head.right);
			}
			// if head.left == null  or
			// if head.left != null head.right == null
			// ==> the next poll node is leaf node
			if (head.left == null || head.right == null){
				leaf = true;
			}
		}
		return true;
	}

2.方法2

2.1准备实体保存该树信息

	public static class Info {
		private boolean isEntirely;
		private boolean isFull;
		private int height;
		public Info(boolean isE,boolean isF, int h){
			isEntirely = isE;
			isFull = isF;
			height = h;
		}
	}

2.2具体调用

	/**
	 * check whether entirely binary tree
	 * function two
	 * @param head
	 * @return
	 */
	public static boolean isEntirely2(Node head) {
		if(head == null){
			return true;
		}
		return process(head).isEntirely;
	}
	public static Info process(Node node) {
		if(node == null){
			return new Info(true,true,0);
		}
		Info leftInfo = process(node.left);
		Info rightInfo = process(node.right);
		// determine the three is full
		boolean isFull = leftInfo.isFull && rightInfo.isFull && leftInfo.height == rightInfo.height;
		// isEntirely
		boolean isEntirely = false;
		if(isFull){
			isEntirely = true;
		}else {
			// both sides are complete binary trees
			if(leftInfo.isEntirely && rightInfo.isEntirely){
				/*
				    a
				   / \
				  b  null

				leftInfo.isEntirely
				rightInfo.isFull
				leftInfo.height == rightInfo.height + 1

				*/
				if(rightInfo.isFull && leftInfo.height == rightInfo.height + 1){
					isEntirely = true;
				}
				/*
				     a
				   /  \
				  b    c
				 / \  / \
				e  f g null

				leftInfo.isFull
				rightInfo.isEntirely
				leftInfo.height == rightInfo.height
				 */
				if(leftInfo.isFull && leftInfo.height == rightInfo.height){
					isEntirely = true;
				}

			}
		}

		int height = Math.max(leftInfo.height,rightInfo.height) + 1;
		return new Info(isEntirely,isFull,height);
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值