Leetcode 331: Verify Preorder Serialization of a Binary Tree

331. Verify Preorder Serialization of a Binary Tree  Total Accepted: 23475 Total Submissions: 68739 Difficulty: Medium Contributors: Admin One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as …

Leetcode 104: Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree  Total Accepted: 184446 Total Submissions: 369369 Difficulty: Easy Contributors: Admin Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.   Code (BFS) from collections import * # Definition …

Leetcode 378: Kth Smallest Element in a Sorted Matrix

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5, 9], [10, 11, 13], [12, …

Leetcode 9: Palindrome Number

https://leetcode.com/problems/palindrome-number/ 9. Palindrome Number   Total Accepted: 156158 Total Submissions: 467127 Difficulty: Easy Contributors: Admin Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using …

199: Binary Tree Right Side View

199. Binary Tree Right Side View Total Accepted: 56659 Total Submissions: 151351 Difficulty: Medium Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <— / \ 2 3 <— \ …

Leetcode 310: Minimum Height Trees

310. Minimum Height Trees   Total Accepted: 20625 Total Submissions: 74043 Difficulty: Medium For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, …

Leetcode 78: Subsets

78. Subsets   Total Accepted: 118197 Total Submissions: 341929 Difficulty: Medium Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Subscribe to see which companies asked …

Leetcode 94: Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal Total Accepted: 151532 Total Submissions: 358835 Difficulty: Medium Given a binary tree, return the inorder traversal of its nodes’ values. For example:Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively?   Code (recursive) # Definition for a binary …