Skip to content

104. Maximum Depth of Binary Tree

难度:Easy

刷题内容

原题连接

  • https://leetcode.com/problems/maximum-depth-of-binary-tree/

内容描述

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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
return its depth = 3.

˼· **- ʱ�临�Ӷ�: O(N)*- �ռ临�Ӷ�: O(N)***

��DFS�ݹ内容描述�����ֱ��ҵ内容描述内容描述内容描述� depth���Ƚ内容描述内容描述� depth�Ĵ�С��

```cpp / * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; / class Solution { public: int maxDepth(TreeNode root) { return max(root,0); } int max(TreeNode* ptr,int deep) { if(ptr != nullptr) { int ldeep = max(ptr ->left,deep + 1); int rdeep = max(ptr ->right,deep + 1); if(ldeep > rdeep) deep = ldeep; else deep = rdeep; } return deep; } };



回到顶部