LeetCode -- Minimum Depth of Binary Tree代码分析

做完这道题,顿时感到自己得数据结构与C++学得是有多矬,一道很简单得二叉数,竟交了一遍又一遍,

在此,我希望能够看到这篇博客得朋友,"有时间就刷一下LeetCode吧,最起码,测一下自己得算法与数据结构水平,

如果实在是跟我一样的话,那就好好学吧,毕竟现在还不完,"想奋斗,什么时候都不晚!!!",与君共勉!


/**  * 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)     {         // IMPORTANT: Please reset any member data you declared, as         // the same Solution instance will be reused for each test case.                  // 空树的情况         if (!root)             return 0;                  // 有根结点,但是左右子树都为空得情况         if (!root -> left && !root -> right)             return 1;                      int l = minDepth(root -> left);         int r = minDepth(root -> right);                  // 如果坐子树为空,这时候就不能直接这样了         // return (l < r ? l+1 : r+1);         // 本人矬就在这里,没有特判这两种没有左||右子树得情况         if (l == 0)             return r+1;         if (r == 0)             return l+1;         // 如果左右子树都存在!!!         return (l < r ? l+1 : r+1);     } };


你可能感兴趣的:(LeetCode,数据结构,C++,算法,代码分析)