Leetcode - Tree - 104. Maximum Depth of Binary Tree(DFS求二叉樹最深深度)

1. Problem 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.

2. My solution

這個題寫的時候沒有IDE,直接在提交框裏寫了竟然1A

class Solution {
private:
    int ans;
public:
    void DFS(TreeNode* root,int ceng)
    {
        if(!root)
            return ;
        if(ceng>ans)
            ans=ceng;
        DFS(root->left,ceng+1);
        DFS(root->right,ceng+1);
    }
    int maxDepth(TreeNode* root) {
        ans=0;
        DFS(root,1);
        return ans;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章