Day2:二叉树的最大深度

leetcode链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

递归的做法:

**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null)return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return (left > right)?left+1:right+1;

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

小白刷Leetcode 文章被收录于专栏

那些必刷的leetcode

全部评论

相关推荐

七牛云头号黑子:人家是过度包装被看出来没过简历,你是包都不包啊兄弟
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务