Leetcode #104 Maximum Depth of Binary Tree

来源:互联网 发布:nb仿真实验室软件 编辑:程序博客网 时间:2024/06/06 02:33

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.

Difficulty:Easy

class Solution {public:    int dep = 1;    void reco(TreeNode* root,int curr){        if(root==NULL)        {            dep = dep>curr?dep:curr;            return;        }        reco(root->left,curr+1);        reco(root->right,curr+1);            }         int maxDepth(TreeNode* root) {        if(root==NULL)           return 0;        reco(root->left,1);        reco(root->right,1);        return dep;                       }};


0 0
原创粉丝点击