LeetCode OJ 104. Maximum Depth of Binary 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.

很简单,直接上代码:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0; int leftmax = maxDepth(root.left);
int rightmax = maxDepth(root.right); return leftmax>rightmax?leftmax+1:rightmax+1;
}
}
上一篇:opensuse编译安装Python3后缺少zlib


下一篇:Jquery实现简单到计时功能(setTimeout,setInterval)