标签:
求二叉树的最大深度。
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
考虑特殊情况:
采用分治法:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int maxDepth(TreeNode root) { int maxDepth = 0; if( root != null ){ int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); maxDepth = (leftDepth>rightDepth ? leftDepth : rightDepth) + 1; } return maxDepth; } }
标签:
原文地址:http://www.cnblogs.com/hf-cherish/p/4598775.html