Minimum Depth of Binary Tree

来源:互联网 发布:点击淘宝延长收货3天 编辑:程序博客网 时间:2024/06/01 10:49
/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int minDepth(TreeNode *root) {        if(root==NULL)        {            return 0;        }        if((root->left==NULL)&&(root->right==NULL))        {            return 1;        }        if(root->left==NULL)        {            int rdepth=minDepth(root->right);            return rdepth+1;        }        else if(root->right==NULL)        {            int ldepth=minDepth(root->left);            return ldepth+1;        }        else        {            int ldepth=minDepth(root->left);            int rdepth=minDepth(root->right);            return ldepth>rdepth?rdepth+1:ldepth+1;           }    }};

0 0
原创粉丝点击