LC 988. Smallest String Starting From Leaf
2024-09-05 06:01:58
Given the root
of a binary tree, each node has a value from 0
to 25
representing the letters 'a'
to 'z'
: a value of 0
represents 'a'
, a value of 1
represents 'b'
, and so on.
Find 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.)
Runtime: 4 ms, faster than 100.00% of C++ online submissions for Smallest String Starting From Leaf.
Memory Usage: 884.7 KB, less than 100.00% of C++ online submissions for Smallest String Starting From Leaf.
class Solution {
public:
string smallestFromLeaf(TreeNode* root) {
vector<string> a;
helper(root, a, "");
sort(a.begin(), a.end());
return a[];
}
void helper(TreeNode* root, vector<string>& a, string parent){
if(!root) return;
string tmpc(,(char)('a'+root->val));
string tmps = tmpc + parent;
if(!root->left && !root->right){
a.push_back(tmps);
return;
}
helper(root->left, a, tmps);
helper(root->right, a, tmps);
}
};
最新文章
- JavaScript中尺寸、坐标
- jquery 触发/失去焦点事件例子详解
- python 实现简单排序
- sqlplus链接数据库报ORA-09925: Unable to create audit trail file
- android mvvm
- [ShortCut] Visual Studio快捷键
- Lighttpd
- 【转载】kafka的工作原理
- iptables使用
- Codeforces Round #337 Vika and Segments
- Android下结束进程的方法
- 【转】adb shell dumpsys 命令
- oracle填坑之PLSQL中文显示为问号
- Java 8 新特性-菜鸟教程 (0) -Java 8 新特性
- 关于GPL协议的理解(开源与商用、免费与收费的理解)
- linux内核分析 第5章读书笔记
- oracle client字符集设置 乱码问题
- Python学习笔记020——数据库中的数据类型
- Oracle数据库学习(二):Oracle Linux下oracle、ogg的挂载与参数配置
- Kail安装后的配置