【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 ...
随机推荐
- OpenWrt刷机
http://blog.163.com/l1_jun/blog/static/14386388201441344612/ http://blog.chinaunix.net/uid-25073805- ...
- hbase的thriftserver开启
说明:hbase的thriftserver默认已经编译好,可以使用,不需要跟hadoopthrift一样配置. 要使用Hbase的thrift接口,必须将它的服务启动,命令行为: hbase-deam ...
- POJ 2010 Moo University - Financial Aid 优先队列
题意:给你c头牛,并给出每头牛的分数和花费,要求你找出其中n(n为奇数)头牛,并使这n头牛的分数的中位数尽可能大,同时这n头牛的总花费不能超过f,否则输出-1. 思路:首先对n头牛按分数进行排序,然后 ...
- pyqt显示指定范围的数字
# -*- coding: cp936 -*- # -*- coding: cp936 -*- import sys from PyQt4 import QtCore, QtGui #导入模块 a ...
- [[UIScreen mainScreen] scale]详解
[[UIScreen mainScreen] scale]详解 当屏幕分别为640x940时[[UIScreen mainScreen] scale]=2.0 当屏幕分别为320x480时[[UISc ...
- java技术学习网址收藏
Bootstrap:http://www.runoob.com/bootstrap/bootstrap-intro.html AngularJS : http://www.runoob.com/ang ...
- Good Teacher(模拟)
Good Teacher Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- XCode中在提示窗体中对已弃用的API接口画上红线
当我们在XCode中写程序时会不断的出现相关API提示窗体,那敲起来是一个爽啊. 有时候会看到一些API已经弃用了被画上红色的横线.说明该接口已经被弃用,仍保留,但不建议使用,对弃用API实现画横线事 ...
- java遍历泛型的方法
一.List遍历 Java中List遍历有三种方法来遍历泛型,主要为: 1.for循环遍历 2.iterator遍历 3.foreach遍历 package com.gmail.lsgjzhuwei; ...
- 【HDU】 1018 Big Number
大意就是求 : log10(n!) = log10(1 * 2 * 3 * .......*n) = log10(1) + log10(2) + ........+log10(n); 打表的话会ML ...