LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java
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 [,,,null,null,,], / \ / \ return its depth =
方法一:采用递归方法:(C++)
①如果一棵树只有一个结点,它的深度为1。
②如果根结点只有左子树而没有右子树,那么树的深度应该是其左子树的深度加1;同样如果根结点只有右子树而没有左子树,那么树的深度应该是其右子树的深度加1。
③如果既有右子树又有左子树,那该树的深度就是其左、右子树深度的较大值再加1。
int maxDepth(TreeNode* root) {
if(!root)
return ;
int nleft=maxDepth(root->left);
int nright=maxDepth(root->right);
return nleft>nright?nleft+:nright+;
}
Java:
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int nleft=maxDepth(root.left);
int nright=maxDepth(root.right);
return nleft>nright?nleft+1:nright+1;
}
方法二:采用迭代方法:利用队列先进先出的特性,将每一层的结点弹出后,再将其左右子树压进队列,直至该层的结点全部弹出(C++)
int maxDepth(TreeNode* root) {
if(!root)
return ;
int res=;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
res++;
for(int i=q.size();i>;i--){
TreeNode* t=q.front();
q.pop();
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
}
return res;
}
Java:
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
Queue<TreeNode> m=new LinkedList<>();
m.offer(root);
int res=0;
while(!m.isEmpty()){
res++;
for(int i=m.size();i>0;i--){
TreeNode t=m.poll();
if(t.left!=null)
m.offer(t.left);
if(t.right!=null)
m.offer(t.right);
}
}
return res;
}
LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/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 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 l ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- Leetcode 104 Maximum Depth of Binary Tree 二叉树
计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 104 Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- [LintCode] 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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- CNN试验记录
CIFAR-10 图像处理:(预处理还是很重要的) 数据随机裁剪,填充0 依概率p水平翻转 1.VGG16 SGD lr=0.01 momentum 0.9 weight_decay=0.0001 e ...
- TEAMWORK1
代码信息 队友:马司雨 队友项目:Prim算法生成最小生成树 codeing地址:这里 博客地址:那里 代码功能审查表 功能模块名称 Prim算法生成最小生成树 审查人 张洋 审查日期 2019.4. ...
- canvas绘制环形进度条
<!DOCTYPE html> <html > <head> <meta http-equiv="content-type" conten ...
- TensorFlow - 在 windows 系统上安装
安装方式: 1.pip (将介绍) 2.Anaconda 我采用的是本地 pip 方式 需提前安装 Python - Python 3.5.x > TF 只支持 Python 3.5.x 版本, ...
- 如何在C++中使用动态三维数组
目录 1. 使用new和delete来构造 2. 使用malloc和free来构造 3.构造函数来生成数组 1. 使用new和delete来构造 在使用new申请内存时,在使用过后,一定要采用dele ...
- C语言 基础
内存的定义 在学习python的时候 了解过内存的管理机制,例如引用计数,垃圾回收,小内存池的概念. 但是并不了解内存究竟是个什么东西?不了解内存的实际存储方式. Mac OS Mojave 处理器 ...
- git编译安装
因yum安装的git版本过低,所以尝试使用编译安装git 以下为编译安装时执行的命令 tar xf git-2.9.5.tar.gz cd git-2.9.5yum install curl-deve ...
- PHP之Trait详解
php从以前到现在一直都是单继承的语言,无法同时从两个基类中继承属性和方法,为了解决这个问题,php出了Trait这个特性 用法:通过在类中使用use 关键字,声明要组合的Trait名称,具体的Tra ...
- 深度系统 deepin 15.9 关闭桌面
深度系统 deepin 15.9 关闭桌面 由于特别的原因,关闭深度的桌面. sudo systemctl disable lightdm 如果需要在命令模式进入桌面可以使用以下命令. sudo se ...
- Ansible运维自动化
Ansible运维自动化 一.Ansible-playbook的初步使用 playbook的使用,playbook可以把ansible的模块进行组合 ln -s /usr/local/python/b ...