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

    xiaoxiao2026-04-17  2

    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; } };
    转载请注明原文地址: https://ju.6miu.com/read-1308945.html
    最新回复(0)