Posts

Showing posts with the label traversal

1428. Leftmost Column with at Least a One

Image
https://leetcode.com/problems/leftmost-column-with-at-least-a-one/ https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3306/ (This problem is an  interactive problem .) A binary matrix means that all elements are  0  or  1 . For each  individual  row of the matrix, this row is sorted in non-decreasing order. Given a row-sorted binary matrix binaryMatrix, return leftmost column index(0-indexed) with at least a  1  in it. If such index doesn't exist, return  -1 . You can't access the Binary Matrix directly.   You may only access the matrix using a  BinaryMatrix  interface: BinaryMatrix.get(x, y)  returns the element of the matrix at index  (x, y)  (0-indexed). BinaryMatrix.dimensions()  returns a list of 2 elements  [n, m] , which means the matrix is  n * m . Submissions making more than  1000  calls to  BinaryMatrix.get  will b...

297. Serialize and Deserialize Binary Tree

https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure. Example:  You may serialize the following tree: 1 / \ 2 3 / \ 4 5 as "[1,2,3,null,null,4,5]" Clarification:  The above format is the same as  how LeetCode serializes a binary tree . You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself. Note:  Do n...

987. Vertical Order Traversal of a Binary Tree

Image
https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/ Given a binary tree, return the  vertical order  traversal of its nodes values. For each node at position  (X, Y) , its left and right children respectively will be at positions  (X-1, Y-1)  and  (X+1, Y-1) . Running a vertical line from  X = -infinity  to  X = +infinity , whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing  Y  coordinates). If two nodes have the same position, then the value of the node that is reported first is the value that is smaller. Return an list of non-empty reports in order of  X  coordinate.  Every report will have a list of values of nodes. Example 1: Input: [3,9,20,null,null,15,7] Output: [[9],[3,15],[20],[7]] Explanation: Without loss of generality, we can assume the root node is at position (0, 0): Then, the...