/*
* Leetcode104. Maximum Depth of Binary Tree
* Funtion: Given a binary tree, find its maximum depth.
* Author: LKJ
* Date:2016/7/19
*/
#include <iostream>
//#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL),right(NULL) {}
};
class Solution {
public:
int maxDepth(TreeNode* root) {//采用DFS的思想
if (NULL == root)
return 0;
int leftdepth = maxDepth(root->left);
int rightdepth = maxDepth(root->right);
return leftdepth > rightdepth ? leftdepth + 1 : rightdepth + 1;
}
};
int main(){
//int myin;
//int myout;
//Solution SA;
//cout << "Please Enter" << endl;
//cin >> myin;
//myout = SA.singleNumber(myin);
//cout << myout << endl;
//cout << getFristBit(1) << endl;
return 0;
}
LeetCode 二叉树 | 104. Maximum Depth of Binary Tree
本文介绍了解决LeetCode 104题——寻找二叉树最大深度的方法。通过递归深度优先搜索(DFS)策略,实现了一个简洁高效的算法来计算给定二叉树的最大深度。

被折叠的 条评论
为什么被折叠?



