minimum-depth-of-binary-tree

来源:互联网 发布:linux的syslog开启 编辑:程序博客网 时间:2024/06/06 14:12

问题描述:

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.


解决方案:

class Solution { public:    int run(TreeNode *root) {        if(root==NULL)return 0;        else {            int i=run(root->left);            int j=run(root->right);            if(i==0||j==0)                return 1+i+j;            else            return (i<j)?i+1:j+1;                    }       }};