maximum-depth-of-binary-tree

来源:互联网 发布:知天气福建版 数值预报 编辑:程序博客网 时间:2024/06/08 10:19

题目:

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.

程序:

class Solution {public:    int maxDepth(TreeNode *root) {        if(root==NULL)            return 0;        int h1=0,h2=0;        if(root->left)            h1=maxDepth(root->left);        if(root->right)            h2=maxDepth(root->right);        return h1>h2?h1+1:h2+1;    }};
原创粉丝点击