Leetcode-988 (從葉結點開始的最小字符串)
阿新 • • 發佈:2019-02-03
eno tor oot leet pan 結點 div pty lee
1 #define pb push_back 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 class Solution 4 { 5 public: 6 void preorder(vector<int> v,TreeNode* root,string &s) 7 { 8 9 if(root->left==NULL&&root->right==NULL) 10 {11 v.pb(root->val); 12 string tmp; 13 for(int i = v.size()-1;i >= 0;i --) 14 tmp += v[i]+‘a‘; 15 if(s.empty()) 16 s = tmp; 17 else if(s > tmp) 18 s = tmp;19 return ; 20 } 21 v.pb(root->val); 22 if(root->left) 23 preorder(v,root->left,s); 24 if(root->right) 25 preorder(v,root->right,s); 26 } 27 string smallestFromLeaf(TreeNode* root)28 { 29 vector<int> v; 30 string s; 31 preorder(v,root,s); 32 return s; 33 } 34 };
Leetcode-988 (從葉結點開始的最小字符串)