当前位置: 首页 > news >正文

Leetcode 988. Smallest String Starting From Leaf (二叉树遍历好题)

  1. Smallest String Starting From Leaf
    Medium
    1.6K
    227
    Companies
    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.

Example 1:

Input: root = [0,1,2,3,4,3,4]
Output: “dba”
Example 2:

Input: root = [25,1,3,1,3,0,2]
Output: “adz”
Example 3:

Input: root = [2,2,1,null,1,0,null,0]
Output: “abc”

Constraints:

The number of nodes in the tree is in the range [1, 8500].
0 <= Node.val <= 25

解法1:遍历二叉树即可。
注意这里是要找到顺序最小的字符串,所以gMaxPath要初始化成ascii很大的字符串。而ascii table里面,数字<大写字母<小写字母。在前128个ascii code里面,比’z’还大的只有’{‘,’}‘,‘|’和DEL。所以可以初始化成"{}"。

/*** 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 smallestFromLeaf(TreeNode* root) {helper(root, "");return gMaxPath;}
private:string gMaxPath = "{}";//'{' or '}' > 'z'void helper(TreeNode *root, string path) {if (!root) return;path += 'a' + root->val;if (!root->left && !root->right) {reverse(path.begin(), path.end());gMaxPath = min(gMaxPath, path);return;}helper(root->left, path);helper(root->right, path);return;}
};

二刷:path改成引用。
需要注意:

  1. 如果不是引用,可以直接调用helper(root, “”),但是如果是引用的话,需要先定义string path = “”, 然后调用helper(root, path)。
  2. 在 if (!root->left && !root->right) {}里面return前也要 path.pop_back();
/*** 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 smallestFromLeaf(TreeNode* root) {string path = "";helper(root, path);return gMaxPath;}
private:string gMaxPath = "{}";//'{' or '}' > 'z'void helper(TreeNode *root, string &path) {if (!root) return;path += 'a' + root->val;if (!root->left && !root->right) {string tmp = path;reverse(tmp.begin(), tmp.end());gMaxPath = min(gMaxPath, tmp);path.pop_back();return;}helper(root->left, path);helper(root->right, path);path.pop_back();return;}
};
http://www.lryc.cn/news/271520.html

相关文章:

  • redis 三主六从高可用docker(不固定ip)
  • 12.26
  • 2022年全国职业院校技能大赛高职组云计算正式赛卷第三场-公有云
  • Python | 机器学习之数据清洗
  • 力扣:509. 斐波那契数(动态规划,附带递归版本) 详细讲解动态规划的思路
  • Python3,压箱底的代码片段,提升工作效率稳稳的。
  • Flowable-升级为7.0.0.M2-第三节
  • JavaWeb——前端之AjaxVue
  • 在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
  • uni-app/vue封装etc车牌照输入,获取键盘按键键值
  • iostat获取IO延迟单位从ms调整us的方案
  • K8s 源码剖析及debug实战之 Kube-Scheduler(四):预选算法详解
  • ES6之解构赋值详解
  • UntiyShader(五)属性、内置文件和变量
  • Pytorch简介
  • 亚马逊云科技Amazon Q,一款基于生成式人工智能的新型助手
  • 骑砍战团MOD开发(29)-module_scenes.py游戏场景
  • ROS学习记录:ROS系统中的激光雷达消息包的数据格式
  • Vue.js和Node.js的关系--类比Java系列
  • 我的笔记本电脑死机问题折腾记录
  • uniApp中uView组件库的丰富布局方法
  • TDD-LTE 寻呼流程
  • TCP中的三次握手和四次挥手
  • NAO.99b海潮模型的详解教程
  • Plantuml之JSON数据语法介绍(二十五)
  • 迅为龙芯2K1000开发板虚拟机 ubuntu 更换下载源
  • 你好!Apache Seata
  • RFC6749-OAuth2.0
  • 【代码解析】代码解析之生成token(1)
  • 牛客网SQL训练5—SQL大厂面试真题