leetcode #100 Maximum Depth of Binary Tree

来源:互联网 发布:淘宝降权后多久恢复 编辑:程序博客网 时间:2024/06/10 01:02

递归实现:

/** * 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;        if(root.left==null&&root.right==null)return 1;        return Math.max(1+maxDepth(root.left),1+maxDepth(root.right));    }}

Fight On!

0 0
原创粉丝点击