求二叉树的深度

来源:互联网 发布:js validate 验证 编辑:程序博客网 时间:2024/04/30 07:39

求二叉树的深度,实现过程如下所示:

package cn.edu.nwu.structs.tree;/** * @author jcm * *时间 2016年9月3日 */public class GetBinaryTreeDepth {public static void main(String[] args) {BinaryTreeNode root = CreateBinaryTree.createBinaryTree();int depth = getBinaryTreeDepth(root);System.out.println(depth);}/** * @author jcm * 求二叉树的深度 * @param root 树根 * @return 返回树的深度 */public static int getBinaryTreeDepth(BinaryTreeNode root){if(root == null){return 0;}int depth = 0;int leftDepth = 0;int rightDepth = 0;leftDepth = getBinaryTreeDepth(root.leftTreeNode);rightDepth = getBinaryTreeDepth(root.rightTreeNode);depth = 1 + (leftDepth>rightDepth?leftDepth:rightDepth);return depth;}}


0 0
原创粉丝点击