剑指offer-二叉树的深度
class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public class Solution { public int TreeDepth(TreeNode root) { return root == null ? 0 : Math.max(TreeDepth(root.left), TreeDepth(root.right))+1; }
/****************************************************/ public static void main(String[] args) { TreeNode t = new TreeNode(1); t.left = new TreeNode(2); t.left.left = new TreeNode(3); t.left.left.left = new TreeNode(4); t.left.left.right = new TreeNode(5); Solution s = new Solution(); System.out.println(s.TreeDepth(t)); } }