Do you have a binary tree with exactly 3 nodes? Check if the value of the root is equal to the sum of the values of its two children with this simple Python code. Learn how to improve the runtime of the code from O(n) to O(1).
Category:
Coding Problems
-
-
A student was trying to create a function that sorts all the letters of a word in Python, but their code was broken. The code had several errors, including using the wrong method to convert the word into a list of letters and trying to sort the output of a function that doesn’t have a sort method. The task was to fix the errors and make the function work. As an added challenge, the goal was to shrink the fixed code into a single line. The correct code uses the list() function to convert the word into a list of letters, uses the sort() method on the list, and then converts the list back into a string using the join() method.