Convert Sorted Array to Binary Search Tree

Description https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Difficulty: 1.0/5.0 star Analysis This is a typical divide-and-conquer problem. We construct the tree recursively. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :...

Search Insert Position

Description https://oj.leetcode.com/problems/search-insert-position/ Difficulty: 1/5.0 star Analysis Ask for implementation of binary search. class Solution { public: int searchInsert(int A[], int n, int target) { int left = 0, right = n -1; while(left <= right){ int mid = (left +...

Remove Duplicates from Sorted List i and ii

Description URL: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ Difficulty: 1.0/5.0 stars for problem i and 1.5/5.0 for problem ii Solution /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *...

Populating Next Right Pointers in Each Node i and ii

Problem Description https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Difficulty: 2.5/5.0 stars Analysis The first thought that came to my mind is BFS, as the next pointers are layer-wise pointers and here is a simple implementation. This solution does utilize the property that the tree...

Maximum Product Subarray

Description https://oj.leetcode.com/problems/maximum-product-subarray/ Difficulty: 2.5/5.0 star Related: https://oj.leetcode.com/problems/maximum-subarray/ Analysis This problem looks like a DP problem, however, it can be solved by purely greedy solution. It is the existence of negative numbers and zeros make this problem non-trivial, so the key...