LeetCode-104.Maxinum 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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
public int maxDepth(TreeNode root) {//树 递归 my
if(null==root){
return 0;
}
int ld=maxDepth(root.left);
int lr = maxDepth(root.right);
int d = ld>lr?(ld+1):(lr+1);
return d;
}
还可以使用BFS
相关题
二叉树的最小深度 LeetCode111 https://www.cnblogs.com/zhacai/p/10598843.html
LeetCode-104.Maxinum Depth of Binary Tree的更多相关文章
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 104. Maximum Depth of Binary Tree
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
- leetcode 104 Maximum Depth of Binary Tree ----- java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Java [Leetcode 104]Maximum Depth of Binary Tree
题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...
- Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Leetcode 104 Maximum Depth of Binary Tree python
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
随机推荐
- [Ubuntu] apt 添加第三方库
1. 方法一:直接在 /etc/apt/sources.list 添加第三方库. $ sudo vi /etc/apt/sources.list 在其中添加: deb http://archive.s ...
- springboot 集成elasticsearch5.4.3
官网上对elasticsearch 的集成用的是spring-data,而且,暂时不支持5.x的版本, 要是想集成5.x的版本,我们只能在pom.xml文件中进行修改,如图: <project ...
- MySql数据库设计表添加字段
当要添加的字段属于整型,需要设置默认值 或者: alter table fp_user_base add hasPwd tinyint(4) not null default 0;
- 【LeetCode OJ】Remove Nth Node From End of List
题目:Given a linked list, remove the nth node from the end of list and return its head. For example: G ...
- PowerDesigner学习笔记
首先是概念模型个物理模型的区别,概念模型,即ConceptualDataMode,是没和具体类型的数据库(如SQL Server.MySql)的对应,是一种抽象的,而物理模型是指定了某种具体类型的数据 ...
- PHP 搜索分词实现代码
<?php /** * @author: xiaojiang 2014-01-08 * php 建立分词树 * */ class Tree{ public $w = ''; public $su ...
- FileReader类和FileWriter类的基本用法示例
package com.example.io; import java.io.File; import java.io.FileReader; import java.io.FileWriter; i ...
- 【小程序+thinkphp5】 用户登陆,返回第三方session3rd
服务器环境: centos7 php7.0 准备工作: 注册小程序,并获取 appid .appsecret 下载微信解密算法sdk : https://mp.weixin.qq.com/debu ...
- 【JavaScript】如何判断一个对象是未定义的?(已解决)
JavaScript中,如果使用了一个未定义的变量,会有这样的错误提示:XXX未定义. 代码中,怎样才能判定一个对象是否定义了呢? 使用 typeof 示例: if("undefined& ...
- Python Extension Packages 下载
Python Extension Packages下载 这个下载源资源丰富,python支持版本从2.x到3.7,从win32到win64位都有支持,是非常好资源. 特别留下记号备查. Index b ...