988. Smallest String Starting From Leaf Medium
1/**
2 * [988] Smallest String Starting From Leaf
3 *
4 * 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'.
5 * Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
6 * As a reminder, any shorter prefix of a string is lexicographically smaller.
7 *
8 * For example, "ab" is lexicographically smaller than "aba".
9 *
10 * A leaf of a node is a node that has no children.
11 *
12 * Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/tree1.png" style="width: 534px; height: 358px;" />
14 * Input: root = [0,1,2,3,4,3,4]
15 * Output: "dba"
16 *
17 * Example 2:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2019/01/30/tree2.png" style="width: 534px; height: 358px;" />
19 * Input: root = [25,1,3,1,3,0,2]
20 * Output: "adz"
21 *
22 * Example 3:
23 * <img alt="" src="https://assets.leetcode.com/uploads/2019/02/01/tree3.png" style="height: 490px; width: 468px;" />
24 * Input: root = [2,2,1,null,1,0,null,0]
25 * Output: "abc"
26 *
27 *
28 * Constraints:
29 *
30 * The number of nodes in the tree is in the range [1, 8500].
31 * 0 <= Node.val <= 25
32 *
33 */
34pub struct Solution {}
35use crate::util::tree::{TreeNode, to_tree};
36
37// problem: https://leetcode.com/problems/smallest-string-starting-from-leaf/
38// discuss: https://leetcode.com/problems/smallest-string-starting-from-leaf/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42// Definition for a binary tree node.
43// #[derive(Debug, PartialEq, Eq)]
44// pub struct TreeNode {
45// pub val: i32,
46// pub left: Option<Rc<RefCell<TreeNode>>>,
47// pub right: Option<Rc<RefCell<TreeNode>>>,
48// }
49//
50// impl TreeNode {
51// #[inline]
52// pub fn new(val: i32) -> Self {
53// TreeNode {
54// val,
55// left: None,
56// right: None
57// }
58// }
59// }
60use std::rc::Rc;
61use std::cell::RefCell;
62impl Solution {
63 pub fn smallest_from_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> String {
64 String::new()
65 }
66}
67
68// submission codes end
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_988() {
76 }
77}
78
Back
© 2025 bowen.ge All Rights Reserved.