Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created February 9, 2019 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/683cfcabfa8db69e469f9f35be5e4a33 to your computer and use it in GitHub Desktop.
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/
/**
* 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