111. Minimum Depth of Binary Tree

来源:互联网 发布:焦点访谈神乎大道堂 编辑:程序博客网 时间:2024/05/17 03:00
/*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.*//** * Definition for a binary tree node. * 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) return 0;        int leftD=minDepth(root->left);        int rightD=minDepth(root->right);        return (leftD==0 || rightD==0) ? leftD+rightD+1 : min(leftD,rightD)+1;    }};
原创粉丝点击