104. Maximum Depth of Binary Tree
2016-03-15 21:26
169 查看
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.
题意:求二叉树的树高。
思路:递归求解。
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
题意:求二叉树的树高。
思路:递归求解。
/** * 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 == NULL) return 0; else{ int left, right; left = maxDepth(root->left) + 1; right = maxDepth(root->right) + 1; return left > right ? left : right; } } };
相关文章推荐
- 运行Hadoop的示例程序WordCount-Running Hadoop Example
- C语言中的运算符
- GD库简介和使用
- hdu 1051 Wooden Sticks
- pku1050 To the Max DP
- 快慢指针判断单链表中是否存在环,存在返回环的起点的值
- 160315、mybatis批量删除
- python学习4 常用内置模块
- w3m - 命令行下的浏览器
- Android服务器——使用TomCat实现软件的版本检测,升级,以及下载更新进度!
- Ubuntu 将本地文件源制作成ISO文件
- UI基本控件
- Bootstrap 2.3版与3.0版的使用区别
- Android服务器——使用TomCat实现软件的版本检测,升级,以及下载更新进度!
- android 路径地址与Uri的相互转换 uri转string
- 结合MapReduce和数据集Combining datasets with MapReduce
- C语言中的变量
- EL表达式(胖先生版)
- java Image大小处理
- 二叉树系列——二叉树的最大距离(即相距最远的两个叶子节点,编程之美,百度面试题)