LeetCode 二叉树 | 104. Maximum Depth of Binary Tree

来源:互联网 发布:做淘宝 编辑:程序博客网 时间:2024/06/05 23:40
/* * 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;}
0 0
原创粉丝点击