Created
April 13, 2017 15:39
-
-
Save s4553711/a4711f53bf51d89e82c46bdd38569ef4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 minDepth(root->left) + 1; | |
if (root->right != NULL && root->left == NULL) return minDepth(root->right) + 1; | |
int levl = minDepth(root->left); | |
int revl = minDepth(root->right); | |
return min(revl, levl) + 1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment