Posts

Showing posts from May, 2020

297. Serialize and Deserialize Binary Tree

Image
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'. Example Example 1: Input:{3,9,20,#,#,15,7} Output:{3,9,20,#,#,15,7} Explanation: Binary tree {3,9,20,#,#,15,7}, denote the following structure: 3 / \ 9 20 / \ 15 7 it will be serialized {3,9,20,#,#,15,7} Example 2: Input:{1,2,3} Output:{1,2,3} Explanation: Binary tree {1,2,3}, denote the following structure: 1 / \ 2 3 it will be serialized {1,2,3} Our data serialization uses BFS traversal. This is just for when you got a Wrong Answer and want to debug the input. Note:  Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless. /**    * Definition for a binary tree node.    * ...

560. Subarray Sum Equals K

Image
Given an array of integers and an integer  k , you need to find the total number of continuous subarrays whose sum equals to the  k . Example Example1 Input: nums = [1,1,1] and k = 2 Output: 2 Explanation: subarray [0,1] and [1,2] Example2 Input: nums = [2,1,-1,1,2] and k = 3 Output: 4 Explanation: subarray [0,1], [1,4], [0,3] and [3,4] class  Solution {   public :        int  subarraySum(vector< int >& nums,  int  k) {            int  count = 0, sum = 0;                      unordered_map< int ,  int > mp{{0, 1}};            for  (auto n: nums) {        ...