104.Maximum Depth of Binary Tree

来源:互联网 发布:js访问函数内部变量 编辑:程序博客网 时间:2024/05/16 18:07

题目链接: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. * */public class MaximumDepthOfBinaryTree {public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}}//38 / 38 test cases passed.//Status: Accepted//Runtime: 222 ms//Submitted: 0 minutes ago    public int maxDepth(TreeNode root) {        if(root == null) return 0;    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;    }public static void main(String[] args) {// TODO Auto-generated method stub}}


0 0