public class TreeNode {
char val;
TreeNode left;
TreeNode right;
public TreeNode(char val) {
this.val=val;
}
}
public class BuildTree {
public static TreeNode buildTree() {
// 先构造结点
TreeNode a = new TreeNode('A');
TreeNode b = new TreeNode('B');
TreeNode c = new TreeNode('C');
TreeNode d = new TreeNode('D');
TreeNode e = new TreeNode('E');
TreeNode f = new TreeNode('F');
TreeNode g = new TreeNode('G');
TreeNode h = new TreeNode('H');
TreeNode i = new TreeNode('I');
TreeNode j = new TreeNode('J');
a.left = b;
a.right = c;
b.left = d;
b.right = null;
c.left = e;
c.right = f;
d.left = null;
d.right = g;
e.left = null;
e.right = null;
f.left = h;
f.right = i;
g.left = j;
g.right = null;
h.left = null;
h.right = null;
i.left = null;
i.right = null;
j.left = null;
j.right = null;
return a;
}
}
//二叉树的深度
public class Tree {
private static int height;
public static int 二叉树的深度(TreeNode root){
if (root==null){
return 0;
}
int leftHeight = 二叉树的深度(root.left);
int rightHeight = 二叉树的深度(root.right);
return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}
public static void main(String[] args) {
TreeNode root= buildTree();
System.out.println("此二叉树的深度为:"+二叉树的深度(root));
}
}