###### tags: `LeetCode` `Tree` `Recursion` `Easy` # LeetCode #104 [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) ### (Easy) 給定一個二元樹,找出其最大深度。 二元樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 --- 使用遞迴法, 傳入節點以及該節點的高度, 若節點為空節點, 則代表該節點的父節點為葉子節點, 此時回傳高度, 其餘節點回傳子節點回傳的高度+1。 --- ``` class Solution { public: int maxDepth(TreeNode* root) { return count(root, 0); } int count(TreeNode* node, int level){ if(!node)return level; return max(count(node->right,level), count(node->left,level))+1; } }; ```