![doubly linked list stack check brackets doubly linked list stack check brackets](https://i.pinimg.com/originals/6b/f4/09/6bf4097d60ea89477517361e931db774.jpg)
We have 2 ways to visit the nodes of a tree in a particular order: To calculate height of tree, we can use recursive: def height(tree): if tree = nil: return 0 return 1 + Max(height(tree.left), height(tree.right)) This is an example of a binary tree, we have Les as root and many other nodes, each node has left and right like: Cathy(left), Sam(right) for root node.
![doubly linked list stack check brackets doubly linked list stack check brackets](https://4.bp.blogspot.com/-oz-RNIoeRb8/WFVxhzRihsI/AAAAAAAAANw/aV0Dk5-4rnYNRFj8NYmqr1YiK0zec_fhQCLcB/w1200-h630-p-k-no-nu/dllNode.png)
For binary tree, a node contains: key, left, right, parent(optional). Tree has a root node and many other nodes, each node contains: key, children, parent(optional). See these visualizations: array-based stack, list-based stack, array-based queue, list-based queue. Queue, also know as FIFO(First In First Out), can be implemented with either a linked list (with tail pointer) or an array and has O(1) for Enqueue, Dequeue, Empty operations. Queue is abstract data type with the following operations:ĭequeue(): removes and returns least recently-added key. With Stack data structure, we can solve this problem easily: def is_balanced(str): Stack stack for char in str: if char in : stack.push(char) else: if stack.empty(): return False top <- stack.pop() if (top = '') or (top = '(' and char != ')'): return False return stack.empty() Queue: We want to check the text input is balanced brackets following:
![doubly linked list stack check brackets doubly linked list stack check brackets](https://www.besanttechnologies.com/wp-content/uploads/2019/12/doubly-linked-list.png)
Let say, we want to a checker for a text editor. Stack, also know as LIFO queues, can be implemented with either an array or linked-list and has O(1) for Push, Pop, Top, Empty operations. Pop(): removes and returns most recently-added key Stack is abstract data-type with following operations. def push_back(key): node <- new node node.key <- key node.next = nil if tail = nil: head <- tail <- node node.prev <- nil else: tail.next <- node node.prev <- tail tail <- node def pop_back(): if head = nil: ERROR: empty list if head = tail: head <- tail <- nil else: tail <- tail.prev tail.next <- nil def add_after(node, key): node2 <- new node node2.key <- key node2.next <- node.next node2.prev <- node node.next < node2 if node2.next != nil: <- node2 if tail = node: tail <- node2 def add_before(node, key): node2 <- new node node2.key <- key node2.next <- node node2.prev <- node.prev node.prev <- node2 if node2.prev != nill: <- node2 if head = node: head <- node2 Stack We have 2 nodes to point to next element and previous element. With double-linked list, we can run these operations with O(1). So we just push (i+1) to our stack.In singly-linked list, we have only one node to point to next element and operations like PopBack and AddBefore take O(n) to run. Solving Approach:ġ.Encountering 'I' demands us to increment the previous digit while for 'D' we need to decrement.Ģ.Traverse for i=0 to i=length of the input string and keep pushing (i+1) to the stack.ģ.Following two cases arise for every index:Ĭase 1: If we have encountered I or we are at the last character of input string,then pop from the stack and add it to the end of the output string until the stack gets empty.Ĭase 2: If we have encountered D, then we want the numbers in decreasing order. OBSERVATION:ġ.There can be atmost 9 digits in the output, as repetition is not allowed.Ģ.To form the minimum possible number, we must place the minimum possible digit at each index.ģ.The output string contains one extra character than the input string, since the first character in the input string requires two characters. The third index is I so in the output, 2 to 5 is increasing order.and so on. The second index is D so in the output, 3 to 2 is decreasing order. See original problem statement here Example: Input:įirst index is I so in the output ,1 to 3 is increasing order. 'I' represents increasing and 'D' represents decreasing. Make the minimum number without repetition of the digits such that it follows the sequence given in the string.