【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 ...
随机推荐
- Binary Search Tree BST Template
Use one queue + size variable public class Solution { public ArrayList<ArrayList<Integer>&g ...
- java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade
2012-10-4 19:50:37 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for se ...
- PHP批量审核ajax jquery
var jQuery = $.noConflict(); // alert(jQuery); jQuery(document).ready(function() { /*批量审核*/ jQuery(' ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- pyqt treeview基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from Py ...
- PHP操作MySQL数据库的相关函数
首先,要分清SQL语句的类型: SQL语句的分类 (1)DDL:Data Define Language,数据定义语言--定义表的列结构 CREATE.DROP.ALTER.TRUNCATE (2)D ...
- 07_DICTIONARY_ACCESSIBILITY
07_DICTIONARY_ACCESSIBILITY 控制对系统权限的限制: TRUE 有相应系统权限,允许访问SYS下的对象. FALSE 确保拥有可以访问任何对象的系统权限,但不可以访问SYS下 ...
- C# 获取配置文件节点值
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSetti ...
- Linux 与 unix shell编程指南——学习笔记
第一章 文件安全与权限 文件访问方式:读,写,执行. 针对用户:文件属主,同组用户,其它用户. 文件权限位最前面的字符代表文件类型,常用的如 d 目录:l 符号链 ...
- Windows7 无法打开ASA SSL VPN和ASDM首页
原文地址:Windows7 无法打开ASA SSL VPN 首页和无法打开 ASDM GUI 页面作者:futhy windows 7 无法打开ASA SSL VPN 和AS ...