LeetCode 刷题之二:寻找二叉树的最大深度

题目为:

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 binary tree
 * 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;
       else if(root.left == null && root.right == null)
            return 1;
       else
            {
                int leftDepth= maxDepth(root.left);
       			int rightDepth = maxDepth(root.right);
                if(leftDepth > rightDepth)
                    return leftDepth +1;
                else
                    return rightDepth +1;
             }

    }
}
时间: 2024-09-20 04:19:14

LeetCode 刷题之二:寻找二叉树的最大深度的相关文章

LeetCode刷题之一:寻找只出现一次的数字

投简历的时候看到了个刷题网站,http://www.nowcoder.com/527604,就做了一套题,现记录下来. 题目为: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it withou

Leetcode刷题记录:构建最大数二叉树

题目要求,题目地址 给定一个不含重复数字的数组,最大二叉树构建规则如下: 1.根是数组中最大的数字 2.左边的子树是最大数字左边的内容 3.右边的子树是最大数字右边的内容 答案 class Solution(object): def constructMaximumBinaryTree(self, nums): """ :type nums: List[int] :rtype: TreeNode """ #print(max(nums)) #pr

LeetCode刷题之三:判断两个二叉树是否相同

题目为: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解题思路:这种题目也是递归操作简单 代码为: /** * Definition for binary tree * pub

Leetcode刷题记录:编码并解码短网址

题目要求 编写一个类,提供两个方法.一个可以将普通的网址编码成短网址,一个可以将短网址还原为普通网址. 参考题解 # 使用随机函数,生成短网址,保存在dict中,避免重复 import random import math import re class Codec: charbase = [x for x in "0123456789abcdefghijklmnopqrstuvwxyz"] urldict = {} longurldict = {} def get_random_ch

bug-这是我写的表达式求值,在编译器中运行是对的,但在刷题系统中却说是错,求打什么呢帮我找找Bug

问题描述 这是我写的表达式求值,在编译器中运行是对的,但在刷题系统中却说是错,求打什么呢帮我找找Bug 2C #include""stdio.h""#include""stdlib.h""#include""malloc.h""#include""string.h""#include""math.h""#de

leetcode第一题java代码报错。求原因?

问题描述 leetcode第一题java代码报错.求原因? package com.hust.ali.test; import java.util.*; /** 给定一个整数数组,发现两个数字,使得它们添加到一个特定的目标数. 函数twoSum应返回两个数字,使得它们加起来的目标,其中索引1必须小于索引2的所有. @author Cat */ public class TwoNumSum { /* @param args */ public static void main(String[] a

[LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level

c++问题-在acm上刷题老是通不过,求大神指点一二,到底问题出在哪里。不胜感激!!!

问题描述 在acm上刷题老是通不过,求大神指点一二,到底问题出在哪里.不胜感激!!! #include #include using namespace std; int main() { int T; int k,t=0; int i, j, n1, n2; char a[1010], b[1010], c[1015]; string d[20], e[20], f[20]; cin>>T; for(k=1; k<=T; k++) { cin>>a>>b; d[

JAVA认证历年真题解析二(附答案)

问题描述 我在网上偶然看到一个网站,这个网站里面的资料非常全,除了一些免费的资料,还有网络视频,觉得非常不错,大家有兴趣或者需要,可以去看看[中华IT学习网]www.100itxx.com内容介绍>>本试卷共有45道题,每题后面都有详细解析.例:1.Whichofthefollowingrangeofshortiscorrect?A.-27--27-1B.0--216-1C.?215--215-1D.?231--231-1翻译下面哪些是short型的取值范围.答案 C解析 短整型的数据类型的长