Friedrich Ewald My Personal Website


  • Leetcode: Binary tree inorder traversal

    The task is to traverse a binary tree inorder. This means, that starting from the root node, nodes should be visited in the following order: left, self, right. With this knowledge it is easy to come up with a recursive algorithm. The only special case that needs to be handled are the leaf nodes that don’t have any children. By still visiting the non-existing children and then returning an empty list ([]) when a None is encountered, the whole algorithm fits into two lines.

    class TreeNode:
      def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
    class Solution:
      def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        # Handle non-existing leaf-nodes (including empty root)
        if root is None:
          return []
        # Visit left - current - right
        return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
    if __name__ == '__main__':
      s = Solution().inorderTraversal
      print(s(TreeNode(1, None, TreeNode(2, TreeNode(3), None))), [1,3,2])
    Runtime: 41 ms, faster than 70.32% of Python3 online submissions for Binary Tree Inorder Traversal. Memory Usage: 13.8 MB, less than 60.08% of Python3 online submissions for Binary Tree Inorder Traversal.

  • Leetcode: Square root of x

    The task is to implement a square root method without using math.sqrt or ** or pow or any similar function. Any digits after the decimal point do not to be returned. This is effectively the floor(sqrt(n)) function. The first idea is to simply try out every number and multiply it with itself until the square root is found. If the next number is greater than n, return the previous number. This is however not very efficient as it has a time complexity of O(sqrt(n)) in every case. To speed this process up, we can use binary search. The idea is to always double the size of the steps until we reach the target number or overshoot. If we overshoot, take half of the number and start at 1 again, then double, and so on.

    Continue reading

  • Leetcode: Add binary

    The task is to add two binary numbers of various length. There is more than one possible solution. First, the basic solution which doesn’t use too many builtin Python functions works as follows: Reverse the strings containing the numbers. Then Iterate in a loop as long as we didn’t reach the end of both strings. Write the current position to a temporary variable if it’s still in the boundaries of the string. Then add a potential overflow and handle it if the sum of a + b + overflow > 1. Finally, check for the overflow at the end and add a 1 and reverse the string again before returning it. This can be solved much simpler with builtin Python functions.

    Continue reading

  • Leetcode: Plus one

    A decimal number is given as a list of integers. The number should be incremented by 1. The idea here is to iterate backwards through the list and start with the smallest digit. This way, this challenge can be solved in O(n). If after adding 1, the mod(10) is 0 it means we had an overflow and need to add one. If we didn’t we’re done and can break the loop. As a final check we need to make sure that there is no overflow in the end which would cause us to prepend the list.

    Continue reading

  • Leetcode: Climbing stairs

    There is a given number of stairs n. Stairs can be either taken two steps at a time or one step at a time. For a height of 1, there is only one possible solution: 1. For a height of 2, there are two solutions: 1-1 or 2.

    Continue reading

Page: 11 of 27