Cracking the coding interview--Q4.1

原文:

Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.

译文:

实现一个函数检查一棵树是否平衡。对于这个问题而言, 平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1。


对于这道题,要审清题意。它并不是让你判断一棵树是否为平衡二叉树。 平衡二叉树的定义为:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1, 并且左右两个子树都是一棵平衡二叉树。 而本题的平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1。 这两个概念是不一样的。例如下图,它是一棵平衡二叉树,但不满足本题的平衡条件。 (叶子结点f和l到根结点的距离之差等于2,不满足题目条件)


实现方法:

可以通过遍历整个二叉树,计算出该二叉树最深深度和最浅深度的叶子结点深度,然后相减即为该二叉树最大差值,如果结果相差大于1,

则说明不平衡,如果小于等于1,则说明平衡。

	/**
	 * 获取树的最高和最低深度
	 */
	public static void getDepth(Node node) {
		if(node != null) {
			curDep ++;
			getDepth(node.left);
			if(node.left == null && node.right == null) {
				if(min == 0) {
					min = curDep;
				} else if(curDep < min) {
					min = curDep;
				}
				if(curDep > max) {
					max = curDep;
				}
			}
			getDepth(node.right);
			curDep --;
		}
	}

package chapter_4_TreesandGraphs;

import java.util.List;
import java.util.Stack;

class Node {
	public Node left;
	public Node right;
	public int data;
	
	public Node(int data) {
		this.data = data;
		this.left = null;
		this.right = null;
	}
}

/**
 * 
 * 实现一个函数检查一棵树是否平衡。对于这个问题而言, 平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1。
 *
 */
public class Question_4_1 {
	private static int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
	private static Node root;
	private static int min = 0;
	private static int max = 0;
	private static int curDep = 0;
	
	public static void createTree() {
		Node[] queue = new Node[array.length]; 
		int head = 0;
		int tail = 0;
		if(array.length == 0) {
			return;
		}
		Node p = new Node(array[0]);
		root = p;
		queue[tail++] = p;
		int arrayIndex = 1;
		while(arrayIndex < array.length) {
			Node node = queue[head++];
			Node leftNode = new Node(array[arrayIndex++]);
			node.left = leftNode;
			queue[tail++] = leftNode;
			if(arrayIndex < array.length) {
				Node rightNode = new Node(array[arrayIndex++]);
				node.right = rightNode;
				queue[tail++] = rightNode;
			}
			
		}
	}
	
	public static void preOrderTraverse(Node node) {
		if(node != null) {
			System.out.print(" " + node.data);
			preOrderTraverse(node.left);
			preOrderTraverse(node.right);
		}
	} 
	
	public static void preOrderTraverseByStack(Node root) {
		if(root == null) {
			return;
		}
		Stack<Node> stack = new Stack<Node>();
		stack.push(root);
		while(!stack.empty()) {
			Node node = stack.pop();
			System.out.print(" " + node.data);
			if(node.right != null) {
				stack.push(node.right);
			}
			if(node.left != null) {
				stack.push(node.left);
			}
		}
	}
	
	public static void inOrderTraverse(Node node) {
		if(node != null) {
			inOrderTraverse(node.left);
			System.out.print(" " + node.data);
			inOrderTraverse(node.right);
		}
	}
	
	/**
	 * 获取树的最高和最低深度
	 */
	public static void getDepth(Node node) {
		if(node != null) {
			curDep ++;
			getDepth(node.left);
			if(node.left == null && node.right == null) {
				if(min == 0) {
					min = curDep;
				} else if(curDep < min) {
					min = curDep;
				}
				if(curDep > max) {
					max = curDep;
				}
			}
			getDepth(node.right);
			curDep --;
		}
	}
	
	public static void postOrderTraverse(Node node) {
		if(node != null) {
			postOrderTraverse(node.left);
			postOrderTraverse(node.right);
			System.out.print(" " + node.data);
		}
	}
	
	public static void main(String args[]) {
		createTree();
		System.out.print("先序遍历:");
		preOrderTraverse(root);
		System.out.println("");
		
		System.out.print("中序遍历:");
		inOrderTraverse(root);
		System.out.println("");
		
		System.out.print("后序遍历:");
		postOrderTraverse(root);
		System.out.println("");
		
		System.out.print("先序遍历使用栈:");
		preOrderTraverseByStack(root);
		System.out.println("");
		
		min = 0;
		max = 0;
		curDep = 0;
		getDepth(root);
		System.out.format("min:%d  max:%d\n", min, max);
		if(max - min > 1) {
			System.out.println("非平衡树");
		} else {
			System.out.println("平衡树");
		}
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值