二叉树的深度 Java

时间:2025-02-19 07:38:20
Node node1 = new Node("a");
Node node2 = new Node("b");
Node node3 = new Node("c");
Node node4 = new Node("d");
Node node5 = new Node("e");
root = node1;
= node2;
= node3;
= node4;
= node5;
}
//求二叉树的深度
int length(Node root){
int depth1;
int depth2;
if(root == null) return 0;
//左子树的深度
depth1 = length();
//右子树的深度
depth2 = length();
if(depth1>depth2)
return depth1+1;
else
return depth2+1;
}
}
public class TestMatch{
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
();
(());
}
}