Created
February 9, 2019 08:55
-
-
Save fpdjsns/683cfcabfa8db69e469f9f35be5e4a33 to your computer and use it in GitHub Desktop.
[leetcode] 988. Smallest String Starting From Leaf : https://leetcode.com/problems/smallest-string-starting-from-leaf/
This file contains 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) {} | |
* }; | |
*/ | |
/* | |
* 시간복잡도 : O( | |
*/ | |
class Solution { | |
private: | |
string ans = ""; | |
public: | |
void findAns(TreeNode* root, string s){ | |
if(root == NULL) return; | |
string tmp; | |
tmp.push_back(root->val + 'a'); | |
s = tmp + s; | |
if(root->left == NULL && root->right == NULL){ | |
if(ans == "" || ans > s){ | |
ans = s; | |
} | |
return; | |
} | |
findAns(root->left, s); | |
findAns(root->right, s); | |
} | |
string smallestFromLeaf(TreeNode* root) { | |
ans = ""; | |
findAns(root, ""); | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment