int minDepth(TreeNode* root) { if(root == NULL) return 0; if(root->left == NULL && root->right == NULL) return 1; if(root->left == NULL) return minDepth(root->right) + 1; if(root->right == NULL) return minDepth(root->left) + 1; return min(minDepth(root->left), minDepth(root->right)) + 1; }
int minDepth(TreeNode* root) { if (root == NULL) return 0; if(root->left == NULL && root->right == NULL){ return 1; } int leftDepth, rightDepth; if (root->left == NULL) return minDepth(root->right) + 1; if (root->right == NULL) return minDepth(root->left) + 1; leftDepth = minDepth(root->left); rightDepth = minDepth(root->right); if (leftDepth <= rightDepth) return leftDepth + 1; else return rightDepth + 1; }
int Solution::minDepth(TreeNode *root) { if(!root) return 0; if(!root->left && !root->right) return 1; bool bHasMin = false; int nMin; if(root->left) { int nDepLeft = minDepth(root->left); bHasMin = true; nMin = nDepLeft; } if(root->right) { int nDepRight = minDepth(root->right); if(!bHasMin) nMin = nDepRight; else nMin = min(nMin, nDepRight); } return nMin + 1; }
int minDepth(TreeNode *root) { if(isRoot == 1 && root == NULL){ theMinDepth = 0; } isRoot = 0; if(root == NULL){ } else{ depth++; if(root->left == NULL && root->right ==NULL){ if(theMinDepth > depth){ theMinDepth = depth; } } minDepth(root->left); minDepth(root->right); depth--; } return theMinDepth; }
int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function int depth = 0; if(root == NULL) { return 0; } if(root->left == NULL && root->right == NULL) { return 1; } if(root->left == NULL) { depth++; return depth + minDepth(root->right); } else if(root->right == NULL) { depth++; return depth + minDepth(root->left); } else { depth++; return depth + min(minDepth(root->left),minDepth(root->right)); } }
int minDepth(TreeNode* root) { if (!root) return 0; if (!root->left) return 1 + minDepth(root->right); if (!root->right) return 1 + minDepth(root->left); return 1 + min( minDepth(root->left), minDepth(root->right)); }
int minDepth(TreeNode* root) { if (root == NULL) { return 0; } if (isLeafNode(root)) { return 1; } // 处理左子树 int leftMinDepth = NOT_LEAF_NODE; // 防止把只有左子节点的二叉树的 minDepth 判断为 1(因为右子树 minDepth 为 0) if (root->left != NULL) { leftMinDepth = minDepth(root->left); } // 处理右子树 int rightMinDepth = NOT_LEAF_NODE; // 防止把只有右子节点的二叉树的 minDepth 判断为 1(因为左子树 minDepth 为 0) if (root->right != NULL) { rightMinDepth = minDepth(root->right); } // 返回:root 只有左子树 if (leftMinDepth != NOT_LEAF_NODE && rightMinDepth == NOT_LEAF_NODE) { return leftMinDepth + 1; } // 返回:root 只有右子树 if (leftMinDepth == NOT_LEAF_NODE && rightMinDepth != NOT_LEAF_NODE) { return rightMinDepth + 1; } // 返回:root 有左右子树 else { // 即 leftMinDepth != NOT_LEAF_NODE && rightMinDepth != NOT_LEAF_NODE return MIN_TWO(leftMinDepth, rightMinDepth) + 1; } }
int minDepth(TreeNode * root, bool hasbrother){ if(!root) return hasbrother ? INT_MAX : 0; return 1 + min( minDepth(root->left, root->right != NULL), minDepth(root->right, root->left != NULL)); }
int minDepth(TreeNode* root) { if (root==NULL) return 0; if (root->left==NULL || root->right==NULL) return 1; int left = minDepth(root->left); int right = minDepth(root->right); return left < right ? left+1 : right+1; }
int minDepth(TreeNode* root) { if (!root) { return 0; } // p // if (!root->left && !root->right) { return 1; } // p // l if (root->left && !root->right) { return minDepth(root->left) + 1; } // p // r if (root->right && !root->left) { return minDepth(root->right) + 1; } // p // l r return std::min(minDepth(root->left), minDepth(root->right)) + 1; }
/** * 二叉树的最小深度,时间复杂度:O(n) 空间复杂度O(n) * * @param root 所求二叉树根节点 * * @return 所求二叉树最小深度 */ int minDepth(TreeNode *root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } int minDepthOfLeft = minDepth(root->left); if (minDepthOfLeft == 0) { minDepthOfLeft = INT_MAX; } int minDepthOfRight = minDepth(root->right); if (minDepthOfRight == 0) { minDepthOfRight = INT_MAX; } if (minDepthOfLeft < minDepthOfRight) { return minDepthOfLeft + 1; } else { return minDepthOfRight + 1; } }
int minDepth(TreeNode* root) { if (root == nullptr) return 0; //remember to check there's no right but only left child and vice versa! if (root->left && !root->right) return minDepth(root->left) + 1; if (root->right && !root->left) return minDepth(root->right) + 1; return min(minDepth(root->left), minDepth(root->right)) + 1; }
int minDepth(TreeNode* root) { if (root == NULL) { return 0; } return 1 + min(minDepth(root->right), minDepth(root->left)); }
// solution 1: recursive int minDepth(TreeNode* root) { if(root == NULL) { return 0; } int lf = minDepth(root->left); int rt = minDepth(root->right); return (lf==0 || rt == 0)?lf+rt+1:min(lf, rt)+1; }
int minDepth(TreeNode *root) { if (root == NULL) return 0; int leftDepth = minDepth(root->left); int rightDepth = minDepth(root->right); if (leftDepth == 0) return rightDepth + 1; if (rightDepth == 0) return leftDepth + 1; return 1 + min(leftDepth, rightDepth); }
int minDepth(TreeNode *root) { if(!root) return 0; if(!root->left && !root->right) return 1; int rst = INT_MAX; if(root->left) rst = minDepth(root->left); if(root->right) rst = min(rst, minDepth(root->right)); return rst + 1; }
int minDepth(TreeNode* root) { if(!root) return 0; int left = minDepth(root->left); int right = minDepth(root->right); if(!left || !right) return max(left, right)+1; return min(left, right)+1; }
int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root==NULL)return 0; int l=minDepth(root->left),r=minDepth(root->right); if(l&&r)return 1+min(l,r); if(l||r)return 1+l+r; return 1; }
int minDepth(TreeNode *root) { if(!root) return; if(!root->left && !root->righ) return 1; int lhs = minDepth(root->left); int rhs = minDepth(root->right); if(0==lhs) return rhs+1; if(0==rhs) return lhs+1; return min(lhs+1,rhs+1); }
int minDepth(TreeNode *root) { if(!root) return 0; // ¿ÕÊ÷ if(!root->left && root->right) // Èô×ó×ÓÊ÷Ϊ¿Õ£¬ÓÒ×ÓÊ÷²»Îª¿Õ return minDepth(root->right)+1; // ·µ»Ø×ó×ÓÊ÷µÄ×î¶Ì·¾¶+1 if(!root->right && root->left) // ͬÀí return minDepth(root->left)+1; return min(minDepth(root->left), minDepth(root->right))+1; // ×óÓÒ¶¼Îª¿Õ»ò¶¼²»Îª¿Õʱ£¬µÝ¹é×ÓÊ÷+1 }
int minDepth(TreeNode* root) { if (root == NULL) return 0; if (root->left != NULL and root->right != NULL) return 1+min(minDepth(root->left), minDepth(root->right)); else if (root->left != NULL) return 1+minDepth(root->left); else if (root->right != NULL) return 1+minDepth(root->right); else return 1; }
int minDepth(TreeNode *root) { if(root == nullptr) return 0; if(root->left == nullptr && root->right == nullptr) return 1; if(root->left == nullptr || root->right == nullptr) { if(root->left != nullptr) return minDepth(root->left)+1; else return minDepth(root->right)+1; } return min(minDepth(root->left), minDepth(root->right))+1; }
unsigned minDepth(TreeNode* node) { if (!node) { return 0; } return 1 + std::min(minDepth(node->left), minDepth(node->right)); }
int minDepth(TreeNode *root) { if(!root) return 0; int nl = minDepth(root->left); int nr = minDepth(root->right); if(nl == 0 || nr == 0) return (nl > nr ? nl : nr) + 1; else return min(nl, nr) + 1; }
/* 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. */ int minDepth(TreeNode *root) { if(!root)return 0; if(!root->left) return minDepth(root->right)+1; else if(!root->right) return minDepth(root->left)+1; else return min(minDepth(root->left),minDepth(root->right))+1; }
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ int minDepth(struct TreeNode* root) { if (!root) return 0; int left = minDepth(root->left); int right = minDepth(root->right); if (left == 0 && right == 0) return 1; else if (left == 0) return right + 1; else if (right == 0) return left + 1; return left < right ? left + 1 : right + 1; }
int minDepth(TreeNode* root) { if (root==nullptr){ return 0; } else if (root->right==nullptr||root->left==nullptr){ return max(minDepth(root->left),minDepth(root->right))+1; } else{ return min(minDepth(root->left),minDepth(root->right))+1; } }
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ int minDepth(struct TreeNode* root) { if (root == NULL) return 0; if (root->left == NULL) return minDepth(root->right) + 1; if (root->right == NULL) return minDepth(root->left) + 1; int dep_l = minDepth(root->left); int dep_r = minDepth(root->right); return ((dep_l < dep_r) ? dep_l : dep_r) + 1; }
int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if (root == NULL) return 0; int d1, d2; d1 = minDepth(root->left); d2 = minDepth(root->right); if (d1 == 0 || d2 == 0) return 1 + max(d1, d2); else return 1 + min(d1, d2); }
int minDepth(TreeNode* root) { if ( NULL == root ) return 0; if ( NULL == root->left && NULL == root->right ) return 1; int left = 0x7FFFFFFF; if ( NULL != root->left ) left = minDepth(root->left); int right = 0x7FFFFFFF; if ( NULL != root->right ) right = minDepth(root->right); return min(left, right) + 1; }