zoukankan      html  css  js  c++  java
  • ⛅104. 二叉树的最大深度

    2020.7.28 LeetCode

    描述

    给定一个二叉树,找出其最大深度。

    二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

    说明: 叶子节点是指没有子节点的节点。

    示例

    给定二叉树 [3,9,20,null,null,15,7],

        3
       / 
      9  20
        /  
       15   7
    
    返回它的最大深度 3 。
    

    题解

    /**
     * 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) {
            return get(root);
        }
        public int get(TreeNode tree){
            if(tree == null){
                return 0;
            }
            int size1= get(tree.left)+1;
            int size2= get(tree.right)+1;
    
            // if(tree.left!=null){
            //     size1= get(tree.left)+1;
            // }else{
            //     size1= 1;
            // }
            // if(tree.right!=null){
            //     size2= get(tree.right)+1;
            // }else{
            //     size2= 1;
            // }
    
            return (size1>size2 ? size1:size2);
        }
    }
    
  • 相关阅读:
    pygame各个模块概述
    安装pygame
    pygame系列
    把字符串转换成整数
    不用加减乘除做加法
    求1+2+3+...+n
    孩子们的游戏(圆圈中最后剩下的数)
    翻转单词顺序列
    左旋转字符串
    和为s的两个数字
  • 原文地址:https://www.cnblogs.com/charlottepl/p/13389515.html
Copyright © 2011-2022 走看看