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.
用recursive走一圈就知道
走到最後發現是null 就回傳0
int maxDepth(struct TreeNode* root) {
if(root==NULL)return 0;
int le=0,ri=0;
le=maxDepth(root->left);
ri=maxDepth(root->right);
return (le > ri)? le+1:ri+1;
}
沒有留言:
張貼留言