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.)

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[0];
  }
  void helper(TreeNode* root, vector<string>& a, string parent){
    if(!root) return;
    string tmpc(1,(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);
  }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10350217.html

时间: 2024-08-04 09:36:31

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

[LeetCode 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 1 represents 'b', and so on. Find the lexicographically smallest string that starts at a leaf of this tre

【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' 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 thi

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

Description You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest. Given the list of strings, output the lexicographically smallest c

CodeForces 632C - C. The Smallest String Concatenation

题意: n 个 串,把他们按照某个次序连起来 , 使连接后的字符串字典序最小. 做这题的时候我简直是蠢死了..... #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> using namespace std; const int maxn = 50010; string str[maxn]; bool cmp(str

codeforces 632C. The Smallest String Concatenation 排序

题目链接 给出n个字符串, 将他们连在一起, 求连玩之后字典序最小的那种情况. 按a+b<b+a排序.... #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #inclu

LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the

[LC] 394. Decode String

Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume tha

[LC] 796. Rotate String

We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can becom

[LC] 767. Reorganize String

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result.  If not possible, return the empty string. Example 1: Input: S = "aab" Out