题目:
二叉树的深度
输入一棵二叉树的根节点,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
分析:
如果一棵树只有一个结点,那么它的深度是1。树的深度是max(根节点左孩子深度,根节点右孩子深度)+1。
解法:
package com.wsy;
class Tree {
private int value;
private Tree left;
private Tree right;
public Tree() {
}
public Tree(int value) {
this.value = value;
this.left = this.right = null;
}
public Tree(int value, Tree left, Tree right) {
this.value = value;
this.left = left;
this.right = right;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Tree getLeft() {
return left;
}
public void setLeft(Tree left) {
this.left = left;
}
public Tree getRight() {
return right;
}
public void setRight(Tree right) {
this.right = right;
}
}
public class Main {
public static int count;
public static void main(String[] args) {
Tree tree = init();
int depth = getDepth(tree);
System.out.println("树的深度是:" + depth);
}
public static Tree init() {
Tree tree7 = new Tree(7);
Tree tree6 = new Tree(6);
Tree tree5 = new Tree(5, tree7, null);
Tree tree4 = new Tree(4);
Tree tree3 = new Tree(3, null, tree6);
Tree tree2 = new Tree(2, tree4, tree5);
Tree tree1 = new Tree(1, tree2, tree3);
return tree1;
}
public static int getDepth(Tree tree) {
if (tree == null) {
return 0;
}
return Math.max(getDepth(tree.getLeft()), getDepth(tree.getRight())) + 1;
}
}
题目:
平衡二叉树
输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某棵二叉树中任意结点的左、右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
分析:
需要重复遍历结点多次的解法,简单但不足以打动面试官
借助求二叉树深度的方法,可以得到每个结点的左右子树深度,判断每个结点左右子树深度相差不超过1,那么就是一棵平衡二叉树。
每个结点只遍历一次的解法,正是面试官喜欢的
如果我们采用后序遍历的方式遍历二叉树的每个结点,在遍历到一个结点时,它的左右子树已经判断过了,只需要查看结果即可。如果有子树不满足平衡了,直接返回,后面的结点也不需要验证了。
解法:
需要重复遍历结点多次的解法,简单但不足以打动面试官
package com.wsy;
class Tree {
private int value;
private Tree left;
private Tree right;
public Tree() {
}
public Tree(int value) {
this.value = value;
this.left = this.right = null;
}
public Tree(int value, Tree left, Tree right) {
this.value = value;
this.left = left;
this.right = right;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Tree getLeft() {
return left;
}
public void setLeft(Tree left) {
this.left = left;
}
public Tree getRight() {
return right;
}
public void setRight(Tree right) {
this.right = right;
}
}
public class Main {
public static void main(String[] args) {
Tree tree = init();
boolean balance = checkBalance(tree);
System.out.println(balance ? "是平衡二叉树" : "不是平衡二叉树");
}
public static Tree init() {
Tree tree7 = new Tree(7);
Tree tree6 = new Tree(6);
Tree tree5 = new Tree(5, tree7, null);
Tree tree4 = new Tree(4);
Tree tree3 = new Tree(3, null, tree6);
Tree tree2 = new Tree(2, tree4, tree5);
Tree tree1 = new Tree(1, tree2, tree3);
return tree1;
}
public static int getDepth(Tree tree) {
if (tree == null) {
return 0;
}
return Math.max(getDepth(tree.getLeft()), getDepth(tree.getRight())) + 1;
}
public static boolean checkBalance(Tree tree) {
if (tree == null) {
return true;
}
int left = getDepth(tree.getLeft());
int right = getDepth(tree.getRight());
int difference = Math.abs(left - right);
if (difference > 1) {
return false;
}
return checkBalance(tree.getLeft()) && checkBalance(tree.getRight());
}
}
每个结点只遍历一次的解法,正是面试官喜欢的
package com.wsy;
class Tree {
private int value;
private Tree left;
private Tree right;
public Tree() {
}
public Tree(int value) {
this.value = value;
this.left = this.right = null;
}
public Tree(int value, Tree left, Tree right) {
this.value = value;
this.left = left;
this.right = right;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Tree getLeft() {
return left;
}
public void setLeft(Tree left) {
this.left = left;
}
public Tree getRight() {
return right;
}
public void setRight(Tree right) {
this.right = right;
}
}
public class Main {
public static void main(String[] args) {
Tree tree = init();
boolean balance = checkBalance(tree) != -1;
System.out.println(balance ? "是平衡二叉树" : "不是平衡二叉树");
}
public static Tree init() {
Tree tree7 = new Tree(7);
Tree tree6 = new Tree(6);
Tree tree5 = new Tree(5, tree7, null);
Tree tree4 = new Tree(4);
Tree tree3 = new Tree(3, null, tree6);
Tree tree2 = new Tree(2, tree4, tree5);
Tree tree1 = new Tree(1, tree2, tree3);
return tree1;
}
public static int checkBalance(Tree tree) {
if (tree == null) {
return 0;
}
int left = checkBalance(tree.getLeft());
if (left == -1) {
return -1;
}
int right = checkBalance(tree.getRight());
if (right == -1) {
return -1;
}
int difference = Math.abs(left - right);
if (difference > 1) {
return -1;
} else {
return 1 + Math.max(left, right);
}
}
}