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 ...
随机推荐
- [MySQL] 变量(参数)的查看和设置 [转]
[MySQL] 变量(参数)的查看和设置 类似于Oracle的参数文件,MySQL的选项文件(如my.cnf)用于配置MySQL服务器,但和Oracle叫法不一样,在MySQL里, 官方叫变量(Var ...
- 史上最全Vim快捷键键位图 -- 入门到进阶
文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) 本文原文地址:http://cenalulu.github.io/linux/all-vim-cheatshe ...
- Oracle数据库的归档模式(archivelog mode)
Oracle数据库可以运行在2种模式下: 归档模式(archivelog) 归档模式可以提高Oracle数据库的可恢复性,生产数据库都应该运行在此模式下,归档模式应该和相应的备份策略相结合,只有归档模 ...
- submit按钮修改宽高的坑
近些天对h5非常感兴趣,边工作边学习,虽然比较累,但过得很踏实.每天都要学习一点东西,这样才能对得起自己.好了,废话不多说,进入今天的主题. 今天遇到了一个非常有趣的东西,就是在修改submit按钮的 ...
- SQL 数据库无法附加,提示 MDF" 已压缩
SQL 数据库无法附加,提示 MDF" 已压缩,但未驻留在只读数据库或文件组中.必须将此文件解压缩 1右键点击数据库所在的文件夹, 2点击属性,在常规选项卡中点击高级, 3在弹出的窗口中 ...
- Python每日一练------内置函数+内置变量+内置模块
1.内置函数 Python所有的内置函数 Built-in Functions abs() divmod() input() open() staticmethod() all() e ...
- 我的Android进阶之旅------>Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
Android 设备的CPU类型通常称为ABIs 问题描述 解决方法 1解决之前的截图 2解决后的截图 3解决方法 4建议 为什么你需要重点关注so文件 App中可能出错的地方 其他地方也可能出错 使 ...
- linux执行run文件显示cannot execute binary file
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- python16_day05【迭代器、生成器、模块】
一.列表生成式 1.我现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],我要求你把列表里的每个值加1,你怎么实现?你可能会想到2种方式 : >>> a ...
- shell sed 命令
1:行首空格 sed 's/^[ \t]*//g' 2:行末空格 sed 's/[ \t]*$//g' 3,删除行首的空格或TAB,并删除<tr>.cat poem2id.txt | ...