-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSmallest String Starting From Leaf.cpp
63 lines (43 loc) · 1.63 KB
/
Smallest String Starting From Leaf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Solution by Rahul Surana
***********************************************************
You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.
Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
As a reminder, any shorter prefix of a string is lexicographically smaller.
For example, "ab" is lexicographically smaller than "aba".
A leaf of a node is a node that has no children.
***********************************************************
*/
#include <bits/stdc++.h>
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
string ans = string(200,'z');
void dfs(TreeNode* root, string b){
if(root == NULL) return;
if(root->left == NULL && root->right == NULL) {
reverse(b.begin(),b.end());
if(ans> string(1,root->val +'a')+b) ans = string(1,root->val +'a')+b;
}
if(root->left) {
dfs(root->left,b+string(1,root->val +'a'));
}
if(root->right) {
dfs(root->right,b+string(1,root->val +'a'));
}
}
string smallestFromLeaf(TreeNode* root) {
dfs(root,"");
return ans;
}
};