已知一棵完全二叉树,求其节点的个数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/as1072966956/article/details/82966454
package class_04;
/**
 * 
 * 已知一棵完全二叉树,求其节点的个数
 * 要求:时间复杂度低于O(N),N为这棵树的节点个数
 *
 */
public class Code_08_CompleteTreeNodeNumber {

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

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

	public static int nodeNum(Node head) {
		if (head == null) {
			return 0;
		}
		return bs(head, 1, mostLeftLevel(head, 1));
	}
	//l为在第几层,h为整个树的层数,h相当于全局变量
	public static int bs(Node node, int l, int h) {
		if (l == h) {
			return 1;
		}
		if (mostLeftLevel(node.right, l + 1) == h) {
			return (1 << (h - l)) + bs(node.right, l + 1, h);
		} else {
			return (1 << (h - l - 1)) + bs(node.left, l + 1, h);
		}
	}

	public static int mostLeftLevel(Node node, int level) {
		while (node != null) {
			level++;
			node = node.left;
		}
		return level - 1;
	}

	public static void main(String[] args) {
		Node head = new Node(1);
		head.left = new Node(2);
		head.right = new Node(3);
		head.left.left = new Node(4);
		head.left.right = new Node(5);
		head.right.left = new Node(6);
		System.out.println(nodeNum(head));

	}

}

猜你喜欢

转载自blog.csdn.net/as1072966956/article/details/82966454
今日推荐