标签:time mat long line art max class esc tree
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.
Java Solution:
Runtime beats 15.89%
完成日期:07/01/2017
关键词:Tree
关键点:post order 来遍历树; Math.max 来取一个更大的值(从两个children返回的depth)
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution 11 { 12 public int maxDepth(TreeNode root) 13 { 14 int depth = 0; 15 16 if(root == null) 17 return depth; 18 19 20 depth = Math.max(maxDepth(root.left), maxDepth(root.right)); 21 22 23 return depth + 1; 24 } 25 }
参考资料:N/A
LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)
标签:time mat long line art max class esc tree
原文地址:http://www.cnblogs.com/jimmycheng/p/7105010.html