参考上一题求深度,记得如果有一边是null,就要返回另一边的深度+1!!!这个不能够省略,否则就wa
/** * 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 == NULL) return 0; if(root -> left == NULL && root -> right == NULL) return 1; int dl = minDepth(root -> left); int dr = minDepth(root -> right); if(dl == 0) return dr + 1; if(dr == 0) return dl + 1; return dl > dr ? dr + 1: dl + 1; }};新闻热点
疑难解答