【LeetCode练习题】Maximum Depth of Binary Tree
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,频率1的题目……用到的知识点是二叉树DFS(深度遍历)。
一个计数变量count,一个记录最大值变量max。
深度遍历的时候,遍历到的节点分三种情况:
- 叶节点。此时比较max和count的大小,将较大的那个赋值给max。
- 非叶节点,且有左节点无右节点,继续遍历其左节点,从左节点返回时要将count减1. 右节点不遍历了。
- 非叶节点,且有右节点无左节点,继续遍历其右节点,从右节点返回时要将count减1. 左节点不便利了。
max即为所求的最大深度。
代码如下:
class Solution {
public:
int maxDepth(TreeNode *root) {
int count = ,max = ;
depth(root,count,max);
return max;
} void depth(TreeNode *root,int &count,int &max){
if(root){
if(!root->left && !root->right){
count++;
if(max < count)
max = count;
}
else{
count++;
if(root->left){
depth(root->left,count,max);
count--;
}
if(root->right){
depth(root->right,count,max);
count--;
}
}
}
}
};
难怪难度系数是1,可以简单成这个样子:
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL) return ; int l = maxDepth(root->left);
int r = maxDepth(root->right); return l > r ? l + : r + ;
}
};
【LeetCode练习题】Maximum Depth of Binary Tree的更多相关文章
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 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 C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- [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 ...
- (二叉树 BFS DFS) 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 题解]: 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 long ...
- 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 ...
- 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 th ...
随机推荐
- JS获取按下的键盘字符
<html> <head> KeyPress Test!<hr> <script language="javascript"> fu ...
- Subsets II 解答
Question Given a collection of integers that might contain duplicates, nums, return all possible sub ...
- 04747_Java语言程序设计(一)_第10章_网络与数据库编程基础
例10.1说明InetAddress类的用法的应用程序. public class Example10_1 { public static void main(String args[]) { try ...
- pyqt labe界面超级链接例子学习
def bz(self): self.lable1=QtGui.QLabel(u'<br><a href=http://windows.microsoft.com/zh-cn/win ...
- codeforces 166C Median - from lanshui_Yang
C. Median time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Udacity调试课笔记之断言异常
Udacity调试课笔记之断言异常 这一单元的内容不是很多,如Zeller教授所说,就是如何写.检查断言,并如何使用工具实现自动推导出断言的条件. 现在,多数的编程语言,尤其是高级编程语言都会有内置的 ...
- hadoop备战:一台x86计算机搭建hadoop的全分布式集群
主要的软硬件配置: x86台式机,window7 64位系统 vb虚拟机(x86的台式机至少是4G内存,才干开3台虚机) centos6.4操作系统 hadoop-1.1.2.tar.gz jdk- ...
- asp.net 读取Excel文档
<注:>默认读取数据从Excel的第二行开始. public DataSet ReadExcel(string Path) { string strConn = "Provide ...
- C++实现二叉树的基本操作
#include <iostream> #include <stack> #include <queue> using std::cin; using std::c ...
- Java访问kafka的时候java.nio.channels.ClosedChannelException解决办法
import java.util.Properties; import kafka.javaapi.producer.Producer; import kafka.producer.KeyedMess ...