Smallest String Starting From Leaf - Leetcode 988 - Python
9,672
Published 2024-04-16
🧑💼 LinkedIn: www.linkedin.com/in/navdeep-singh-3aaa14161/
🐦 Twitter: twitter.com/neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 - HashMap - Python
Problem Link: leetcode.com/problems/smallest-string-starting-fro…
0:00 - Read the problem
0:08 - Drawing Explanation
5:38 - Coding Explanation
leetcode 988
#neetcode #leetcode #python
All Comments (21)
-
even if I solve a problem, I immediately go to see your explanation, so many of leetcode’s editorials are indecipherable (at least for me)
-
This is today's leetcode problem. Nice explanation man!
-
Thank you so much. This was so beautifully explained.
-
Thanks for uploading the video! It motivates me. Consistency is the key! 🔑
-
can you make a series dedicated to trees and related concepts?
-
thank you for your consistency
-
Today is the day. I'm tired of sitting here with this degree and being unable to use it. No more wasted potential. I am buying the 1 yr subscription right now, and I'm going all in to prepare for job hunting. Thank you for all that you do.
-
Thank you! 👍👍
-
That usage of min() was brilliant!!
-
Thank you so much
-
Man this is a good solution.
-
Hey, I think it would be a good idea to make a webextension which enables us to see this video in the leetcode tab itself!
-
I have a question, for the input - [25, 1, 3, 1, 3, 0, 2] shouldn't the output be "bbz" and not "adz" (actual output). Your input is appreciated. thanks! @NeetCodeIO
-
🎉🎉
-
Nice one, good to know, that I am the first here
-
may I ask what highlighting/writing software?
-
Neetcide, I request you to please solve the concurrency problems on leetcode. The leetcode numbers are 1114, 1115, 1116, 1117, 1188, 1195, 1226, 1242, 1279.
-
im pretty sure you dont need the if not root: return part because you never have node
-
Less code but same concept class Solution: def smallestFromLeaf(self, root: Optional[TreeNode]) -> str: self.ans="{" def dfs(node, word): if not node: return word = chr(97 + node.val)+word if not node.left and not node.right: if wordI wrote mine a little differently class Solution { String rc = ""; public String smallestFromLeaf(TreeNode root) { dfs(root,""); return rc; } void dfs(TreeNode root, String temp) { if ( root == null ) return; temp = (char) ('a'+root.val)+temp; if (root.left == null && root.right == null ) { if ( rc.equals("") || temp.compareTo(rc) < 0 ) rc = temp; return; } dfs(root.left,temp); dfs(root.right,temp); } }