Maximum Depth of Binary Tree

来源:互联网 发布:hdmi矩阵使用方法 编辑:程序博客网 时间:2024/05/18 02:56

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.

Subscribe to see which companies asked this question

struct TreeNode {
      int val;
     struct TreeNode *left;
     struct TreeNode *right;
}

int maxDepth(struct TreeNode* root) {
 
int count=0;
int h1,h2;
if(!root)//空树
return 0;
h1=maxDepth(root->left);
h2=maxDepth(root->right);
return h1>h2?h1+1:h2+1;
}

0 0
原创粉丝点击