2017.11.5 LeetCode

来源:互联网 发布:全聚合电视直播软件 编辑:程序博客网 时间:2024/06/02 21:08

104. Maximum Depth of Binary Tree

Description

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.

题意: 求一颗二叉树的深度

分析: 直接dfs模拟即可

参考函数

class Solution {public:    int res = 0;    void dfs(TreeNode* root,int c) {        if(!root) return ;        res = max(res,c);        dfs(root->left,c+1);        dfs(root->right,c+1);    }    int maxDepth(TreeNode* root) {        dfs(root,1);        return res;    }};