LeetCode——Maximum Depth of Binary Tree
LeetCode——Maximum Depth of Binary Tree
Question
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.
Answer
/**
* 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)
return 0;
else {
int i = maxDepth(root->left);
int j = maxDepth(root->right);
return max(i, j) + 1;
}
}
};
LeetCode——Maximum Depth of Binary Tree的更多相关文章
- [LeetCode] 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 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] 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] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode:Maximum Depth of Binary Tree【Python版】
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode:Maximum Depth of Binary Tree_104
LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...
随机推荐
- CSS 伪元素 使用参考
伪元素可以做得事情是非常多的,详情大家可以参考这里 大放异彩的伪元素——可以做什么? 本篇主要讲两个伪元素:before和:after的几个要点: 1.:before和:after是加在元素的里面,也 ...
- 动态代理:JDK原生动态代理(Java Proxy)和CGLIB动态代理原理+附静态态代理
本文只是对原文的梳理总结,以及自行理解.自己总结的比较简单,而且不深入,不如直接看原文.不过自己梳理一遍更有助于理解. 详细可参考原文:http://www.cnblogs.com/Carpenter ...
- Powershell 脚本调用方法
方法一: Get-Content "D:\PowershellScripts\Test\ErrorMailNotice.ps1" | Invoke-Expression 方法二: ...
- a标签的href和onclick
1.链接的onclick事件被先执行,href的事件后执行 2.就今天遇到的问题说一下,通过a标签的onclick事件实现点击后定位到指定的div,或者是执行其他函数(比如弹出artdialog等), ...
- java File delete() 失败,又没有报错。
因为该文件流还没关闭,就执行了delete(),所以删除失败. 先举几个可以删除掉文件和删除不掉文件的例子(先在F盘创建test1.txt文件,然后可以直接拷贝代码到IDE执行),最后总结下原因: 例 ...
- java maven项目打包
使用IDEA打包,首先调出来maven projects窗口: 菜单栏Help->Find Action(Ctrl+Shift+A),输入Maven projects https://blog. ...
- django博客项目1.环境搭建
安装 Python Windows 下安装 Python 非常简单,去 Python 官方网站找到 Python 3 的下载地址,根据你的系统选择 32 位或者 64 位的安装包,下载好后双击安装即可 ...
- Unity3d依赖于平台的编译
Unity的这一功能被命名为"依赖于平台的编译". 这包括了一些预编译处理指令,让你能够专门的针对不同的平台分开编译和运行一段代码. 此外,你能够在编辑器下运行一些代码用于測试而不 ...
- java 程序cpu100%问题
找到java应用进程 ID即 java_id 找到该 java_id对应的CPU占用比较大的线程 ID即 thread_id 使用jdk自带jstack工具打印跟该线程相关的堆栈信息 [root@pv ...
- Docker 网络之端口绑定
外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P 或 -p 参数来指定端口映射. -P 标记时 Docker 会随机映射一个 49000~49900 的端口到内部容 ...