leetcode 104
104. Maximum 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.
求二叉树的最大深度。
采用递归实现:
(1)如果二叉树为空,二叉树的深度为0
(2)如果二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1
代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
{
return ;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right); return left > right ? (left + ) : (right + );
}
};
leetcode 104的更多相关文章
- 递归的三部解题曲 关联leetcode 104. 二叉树最大深度练习
递归关心的三点 1. 递归的终止条件 2. 一级递归需要做什么 3. 返回给上一级递归的返回值是什么 递归三部曲 1. 找到递归的终止条件:递归什么时候结束 2. 本级递归做什么:在这级递归中应当完成 ...
- [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)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- 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 C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
104: class Solution { public: int maxDepth(TreeNode* root) { if(root == NULL) ; int left = maxDepth( ...
- Java实现 LeetCode 104 二叉树的最大深度
104. 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...
- 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 ...
随机推荐
- 大神:python怎么爬取js的页面
大神:python怎么爬取js的页面 可以试试抓包看看它请求了哪些东西, 很多时候可以绕过网页直接请求后面的API 实在不行就上 selenium (selenium大法好) selenium和pha ...
- vim 上下左右变成ABCD 解决办法
一.缘由 一次发现VIM编辑文件,发现上下左右键变输入ABCD.猜测vimrc没设置好. 二.解决办法: ls -l /etc/ |grep vim,发现有两个结果vimrc.rpmnew,vimrc ...
- JAVA类的构造方法
1,构造方法没有返回类型, 定义: []public] 方法名() {} 2,一个构造方法如果想调用同一类中的另一个构造方法,只能调用一个,并且要放在构造方法第一行 3,用this调用,如 publi ...
- 转--一款漂亮实用的Android开源日期控件timessquare
这个开源控件可以兼容到SDK8版本,可以自定义显示的年月日,以及时间范围,如图 如果我们只想显示两个月的日期选择区间: final Calendar month = Calendar.getInsta ...
- linux下zip文件解压后乱码解决方案
解决办法一,利用pyton来处理 1.vi uzip文件2.复制一下内容(Python) #!/usr/bin/env python # -*- coding: utf-8 -*- # uzip.py ...
- NPOI高效匯出Excel
using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using NPOI. ...
- C++学习19 类的多继承
在前面的例子中,派生类都只有一个基类,称为单继承.除此之外,C++也支持多继承,即一个派生类可以有两个或多个基类. 多继承容易让代码逻辑复杂.思路混乱,一直备受争议,中小型项目中较少使用,后来的 Ja ...
- Java中的观察者模式
让一个类能够被观察,则该类需要继承java.util.Observable类. 要让一个类成为观察者,则该类需要实现java.util.Observable接口. 让观察者和被观察者建立联系通过Obs ...
- android studio 更新 Gradle错误解决方法
Android Studio每次更新版本都会更新Gradle这个插件,但由于长城的问题每次更新都是失败,又是停止在Refreshing Gradle Project ,有时新建项目的时候报 Gradl ...
- ownDocument和documentElement
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...