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 ...
随机推荐
- 每一行最后添加文字python脚本
比较简单的在pycharm上跑的脚本 #_*_coding:utf-8_*_ #普通版 file = open("oldfile.txt","r",newlin ...
- 【BZOJ1067】[SCOI2007]降雨量 RMQ+特判
[BZOJ1067][SCOI2007]降雨量 Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年 ...
- QQ能上,网页打不开
这是一个老问题了,在大学的时候就经常碰到有人问这样的问题,今天写出来祭奠一下,姑凉长点心吧~! 安阳地区DNS:网通202.102.224.68 如果你是电信:222.88.88.88或者直接弄成顶级 ...
- 微软Build 2017开发者大会午夜趴
时间:2017年5月10号半夜 地点:微软中关村会议室 一年一度的Build大会,微软今年特地组织了一波粉丝到“现场”远程观摩keynote直播,同时在新浪直播间里也有相应的专家进行同步翻译和讲(tu ...
- Go & SQLite on Windows
一般golang使用的sqlite驱动包都是github.com/mattn/go-sqlite3,但是官方并没有直接支持windows平台的编译,因为windows平台编译默认需要gcc支持 其实解 ...
- MFC 单选按钮组向导添加和动态创建
单选按钮组的动态生成 单选按钮也属于CButton类,但由于单选按钮总是成组使用的,所以它在制作和使用上与普通按钮有一定区别. 假设有三个单选按钮组成一组,初始时,第一个单选按钮处于选中状态. 我们先 ...
- 微信公众号 待发货-物流中-已收货 foreach break continue
w <?php $warr = array(1,2,3); $w_break = 0; foreach($warr AS $w){ if($w==2)break; $w_break += $w; ...
- awesome-modern-cpp
Awesome Modern C++ A collection of resources on modern C++. The goal is to collect a list of resouce ...
- 删除DOM元素 parent.removeChild(target)
p.removeChild(p.children[0]);
- C++设计模式 -- 解析和实现
原文地址 http://c.chinaitlab.com/special/sjms/Index.html#a 导航目录 ※ 设计模式解析和实现之一-Factory模式 ※ 设计模式解析和实现之八-C ...