LC 988. 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 '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.)
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的更多相关文章
- LeetCode 988. Smallest String Starting From Leaf
原题链接在这里:https://leetcode.com/problems/smallest-string-starting-from-leaf/ 题目: Given the root of a bi ...
- 【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 ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [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 ...
- C. The Smallest String Concatenation
C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...
- codeforces 632C The Smallest String Concatenation
The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...
- 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 ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation 排序
C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...
- 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 ...
随机推荐
- pcntl
<?php function my_pcntl_wait($childProcessCode){ $pid = pcntl_fork(); if($pid>0){ pcntl_wait($ ...
- Codeforces Round #590 (Div. 3) 万恶的自己WAC
C题:一道简单的C题卡了半天,我太菜了 题意:给你一个n*2的矩阵,每个位置有一个数字,对应一种管道,要求通道可不可以从左上通到右下 由提议可以看出,1,2对应的是直管道,3,4,5,6对应弯管道,只 ...
- Python 编码encode()、 解码decode()问题
乱码这种东西,时不时出现.本来开开心心想着我要学习啦,然后兴高采烈打开了比火星文还火星文的字符-- 没事,我可以搞定这堆鬼画符. 先来讲一下为什么有乱码这种东西的存在 故事是这样滴: 字符串是Pyth ...
- Java 基础 面向对象- 成员内部类/局部内部类/举例Comparable 接口的匿名内部类
笔记: package 任务135; /**类的 内部类, *1.相当于说, 我们可以在类的内部再定义类, * 2.成员内部类: * a.是外部类的一个成员,4个修饰符:static, final , ...
- resultSet.getMetaData() 获得表结构
1.得到查询结果,一个数据集 rs = stat.executeQuery("select * from " + table_name + " limit " ...
- js获取链接?后边的参数名称或者值
1.获取后边的参数名称<script type="text/javascript"> var url = location.search; //获取url中" ...
- 使用jdk自带的线程池。加载10个线程。
在开发中使用线程,经常不经意间就new Thread()一个出来,然后发现,这样做不是很好,特别是很多线程同时处理的时候,会出现CPU被用光导致机器假死,线程运行完成自动销毁后,又复活的情况. 所以在 ...
- springboot2.0入门(九)-- springboot使用mybatis-generator自动代码生成
一.配置文件引入 插件引入,引入 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId& ...
- for迭代多个对象
1.传统方法 from random import randint chinese = [randint(60, 100) for _ in range(40)] math = [randint(60 ...
- 【leetcode】1240. Tiling a Rectangle with the Fewest Squares
题目如下: Given a rectangle of size n x m, find the minimum number of integer-sided squares that tile th ...