Input binary tree root, the depth of the tree request.
Forming a path tree from the root node to the leaf node sequentially passes (including the root, leaf nodes), the depth of the length of the longest path in the tree.
Sample
input: binary tree [8, 12, 2, null , null, 6, 4, null, null, null, null] as follows:
8
/
122
/
64
Output: 3
class Solution {
public:
int treeDepth(TreeNode* root) {
if(!root) return 0;
int ans = dfs(root,1);
return ans;
}
int dfs(TreeNode *root,int dep) {
int ans = dep;
if(root -> left) ans = max(ans,dfs(root -> left,dep + 1));
if(root -> right) ans = max(ans,dfs(root -> right,dep + 1));
return ans;
}
};