剑指offer55题

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

package com.algorithm05;

import com.tools.TreeBinaryFunction;
import com.tools.TreeNode;

public class Algorithm55 {

	public static int TreeDepth(TreeNode root) {
        
		if(root==null)
			return 0;
		int left,right;
		left = TreeDepth(root.left);
		right = TreeDepth(root.right);
		
		return (left>right)?(left+1):(right+1);
    }
	public static void main(String[] args) {
		TreeNode treeNode = new TreeNode(0);
		TreeBinaryFunction.CreateTreeBinary(treeNode);
		System.err.println(TreeDepth(treeNode));
	}
}

  

原文地址:https://www.cnblogs.com/CloudStrife/p/7434981.html