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 1represents '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);
}
};

LC 988. Smallest String Starting From Leaf的更多相关文章

  1. LeetCode 988. Smallest String Starting From Leaf

    原题链接在这里:https://leetcode.com/problems/smallest-string-starting-from-leaf/ 题目: Given the root of a bi ...

  2. 【leetcode】988. Smallest String Starting From Leaf

    题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a ...

  3. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. [Swift]LeetCode988. 从叶结点开始的最小字符串 | Smallest String Starting From Leaf

    Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to  ...

  5. C. The Smallest String Concatenation

    C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...

  6. codeforces 632C The Smallest String Concatenation

    The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...

  7. CodeForces 632C The Smallest String Concatenation//用string和sort就好了&&string的基础用法

    Description You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them togethe ...

  8. Educational Codeforces Round 9 C. The Smallest String Concatenation 排序

    C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...

  9. codeforces 632C C. The Smallest String Concatenation(sort)

    C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...

随机推荐

  1. CSS基础 布局

    1.布局的基本位置 top         距离上边的距离right       距离右边的距离bottom   距离下边的距离left         距离左边的距离 去掉布局时 html 的3mm ...

  2. wakelock查看

    Android的wakelock分为两层 待机异常https://wenku.baidu.com/view/6b765c8802020740be1e9bd8.html Linux层和应用层 查看Lin ...

  3. 【转】5种网络IO模型

    5种网络IO模型(有图,很清楚) IO多路复用—由Redis的IO多路复用yinch Linux中对文件描述符的操作(FD_ZERO.FD_SET.FD_CLR.FD_ISSET

  4. jmeter分布式运行

    一.设置windows远程启动 1).Jmeter分布式测试时,选择其中一台作为控制机(Controller),其它机器做为代理机(Agent). 2).执行时,Controller会把脚本发送到每台 ...

  5. IIS设置网站为HTTPS并且将HTTP重定向到HTTPS

    第一步:下载证书,导入证书到IIS https://help.aliyun.com/knowledge_detail/95502.html 站点绑定https 第二部:安装URL重写模块 rewrit ...

  6. python----四种内置数据结构(dict、list、tuple、set)

    1.dict 无序,可更改 2.tuple 有序,不可更改 3.list 有序,可更改(增加,删除) 4.set 无序,可能改 {元素1,元素2,元素3.....}和字典一样都是用大括号定义,不过不同 ...

  7. jQuery遍历not的用法

    从包含所有段落的集合中删除 id 为 "selected" 的段落: $("p").not("#selected") 定义和用法 not() ...

  8. 关键字local、global和内置函数【locals、globals】

    每个函数都有着自已的命名空间,叫做局部名字空间,它记录了函数的变量,包括函数的参数和局部定义的变量.每个模块拥有它自已的命名空间,叫做全局命名空间,它记录了模块的变量,包括函数.类.其它导入的模块.模 ...

  9. 2019牛客多校第五场generator2——BSGS&&手写Hash

    题目 几乎原题 BZOJ3122题解 分析 先推一波公式,然后除去特殊情况分类讨论,剩下就是形如 $a^i \equiv b(mod \ p)$ 的方程,可以使用BSGS算法. 在标准的BSGS中,内 ...

  10. java线程安全与不安全的理解

    存在成员变量(全局变量)的类用于多线程时是不安全的,不安全体现在这个成员变量可能发生非原子性的操作,而变量定义在方法内也就是局部变量是线程安全的. 想想在使用struts1时,不推荐创建成员变量,因为 ...