Posts

Flatten Binary Tree to Linked List II

Image
Another problem where you either have to flatten the binary tree into a linked list, or push it into a queue. I decided to do the former - it is easy to have a list of list of nodes, where each index is the level of the tree, and each list is the list of nodes in that level. If you do a pre-order depth-first search (DFS) you can accomplish that. Second part of the problem is the calculation based on the given rules. Just go thru the flatten list, and to the list of nodes, and process based on the rules. If you look at the code down below, it achieves a high degree of symmetry which is usually a trademark when operating with trees. Cheers, ACC. Zigzag Level Sum of Binary Tree - LeetCode You are given the  root  of a  binary tree . Traverse the tree level by level using a zigzag pattern: At  odd -numbered levels (1-indexed), traverse nodes from  left to right . At  even -numbered levels, traverse nodes from  right to left . While traversing a level in th...

Hashtables and Hashsets

Image
Not the best solution for this problem, which also has a lot of corner cases, but the use of Hashtables and Hashsets help to achieve the goal. Whenever you have a choice, prefer Hashsets over Hashtables, and even the latter isn't super performing due to the number of casts needed. Also, in addition the slowdown here is exacerbated by the use of strings (and conversions) manipulations for the order keys. Lots of room for improvement for sure, but it is a good problem to exercise Hashtables and Hashsets in general. Code is down below, cheers, ACC. Design Order Management System - LeetCode You are asked to design a simple order management system for a trading platform. Each order is associated with an  orderId , an  orderType  ( "buy"  or  "sell" ), and a  price . An order is considered  active  unless it is canceled. Implement the  OrderManagementSystem  class: OrderManagementSystem() : Initializes the order management system. void addOrde...