求最小深度/根节点到最近叶子节点的最短路径上的节点数量;c++
尝试考虑:函数
在求解的过程当中下落到某个树\(t\)时spa
若是\(t\)为空, 那么它的最小深度为0;code
若是\(t\)的左子树为空或右子树为空, 那么它的最小深度等于1;递归
能够写出以下代码内存
class Solution { public: int minDepth(TreeNode* root) { if(root==nullptr) return 0; if(root->left!=nullptr && root->right!=nullptr) return 1 + min(minDepth(root->left), minDepth(root->right)); return 1; } };
这显然是错误的, 例如:leetcode
输入: [1,2] 输出 1 预期结果 2
PS: 实际上上面的三条规则是冗余的, 能够化简为:get
若是\(t\)为空, 那么它的最小深度为0;io
不然它的最小深度等于\(1 + min(h_l, h_r)\);class
能够让被抛给递归函数的t确保为非空;
class Solution { public: int helper(TreeNode* t) { if(t->left!=nullptr && t->right!=nullptr) return 1 + min(minDepth(t->left), minDepth(t->right)); if(t->left==nullptr && t->right==nullptr) return 1; return 1 + minDepth((t->left!=nullptr) ? t->left : t->right); } int minDepth(TreeNode* root) { if(root==nullptr) return 0; return helper(root); } };
执行用时 :20 ms, 在全部 C++ 提交中击败了35.52%的用户 内存消耗 :21.6 MB, 在全部 C++ 提交中击败了5.10%的用户
官方题解
class Solution { public: int minDepth(TreeNode* root) { if(root==nullptr) return 0; if(root->left==nullptr && root->right==nullptr) return 1; int minD = INT_MAX; if(root->left!=nullptr) minD = min(minD, minDepth(root->left)); if(root->right!=nullptr) minD = min(minD, minDepth(root->right)); return 1+minD; } };
执行用时 :20 ms, 在全部 C++ 提交中击败了35.52%的用户 内存消耗 :22.1 MB, 在全部 C++ 提交中击败了5.10%的用户
求一棵树的最大深度过程当中, 咱们记得层序遍历的时候, 能够得到当前遍历的深度, 因此直接能够得出最大深度;
而在层序遍历时, 也能够知道当前这个节点是否是叶子节点;
class Solution { public: int minDepth(TreeNode* root) { if(root==nullptr) return 0; int minD = INT_MAX; deque<TreeNode*> q; q.push_back(root); int curDepth = 1; while(!q.empty()) { int n = q.size(); for(int i=0; i<n; ++i) { TreeNode* t = q.front(); q.pop_front(); if(t->left==nullptr && t->right==nullptr) minD = min(minD, curDepth); if(t->left!=nullptr) q.push_back(t->left); if(t->right!=nullptr) q.push_back(t->right); } ++curDepth; } return minD; } };
效率都差很少
执行用时 :20 ms, 在全部 C++ 提交中击败了35.52%的用户 内存消耗 :21.3 MB, 在全部 C++ 提交中击败了5.10%的用户
上面的方法还有一处愚蠢:
class Solution { public: int minDepth(TreeNode* root) { if(root==nullptr) return 0; int minD = INT_MAX; deque<TreeNode*> q; q.push_back(root); int curDepth = 1; while(!q.empty()) { int n = q.size(); for(int i=0; i<n; ++i) { TreeNode* t = q.front(); q.pop_front(); if(t->left==nullptr && t->right==nullptr) { minD = min(minD, curDepth); break; /// #找到第一个叶子即完成 } if(t->left!=nullptr) q.push_back(t->left); if(t->right!=nullptr) q.push_back(t->right); } ++curDepth; } return minD; } };
快了很多
执行用时 :12 ms, 在全部 C++ 提交中击败了85.32%的用户 内存消耗 :21.4 MB, 在全部 C++ 提交中击败了5.10%的用户