LeetCode:Maximum Depth of Binary Tree
2015-08-17 10:49
351 查看
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; if(!root->left&&!root->right) return 1; int leftDepth=maxDepth(root->left); int rightDepth=maxDepth(root->right); if(leftDepth==0) return rightDepth+1; if(rightDepth==0) return leftDepth+1; else return max(rightDepth,leftDepth)+1; } };
相关文章推荐
- UIWebView与JS的深度交互
- UIWebView与JS的深度交互
- springmvc国际化实现方法
- where clause is ambiguous
- UNIX网络编程卷一:第二十一章 多播
- 随机发生器
- Android Volley完全解析(一),初识Volley的基本用法
- (转)js中sort()方法的用法,参数以及排序原理
- ffmpeg 笔记 二:编译安装ffmpeg库
- ZOJ 3703 Happy Programming Contest(0-1背包)
- 测试分析例子--杯子,圆珠笔
- 制作cocoapods国内镜像
- JavaScript 无符号位移运算符 >>> 三个大于号 的使用方法
- 終戦の日、各党が談話発表 安保法案への賛否にも言及
- leetcode 147 —— Insertion Sort List
- 单选 性别选项(男,女)提交问题
- Eclipse编译调试Cocos2d问题汇总
- php面向对象要点[转]
- 101 Symmetric Tree
- 平衡