Best Time to Buy and Sell Stock II (C# and Python3)

You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day. Find and return the maximum profit you can … Read more

Best Time to Buy and Sell Stock (C# and Python3)

You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example 1: Input: … Read more

Construct Binary Tree from Preorder and Inorder Traversal (C# and Python3)

Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. Reminder: BST property Solution using preorder inorder and recursion Inorder is nothing but LEFT ROOT RIGHT TraversalPreorder is nothing but ROOT LEFT RIGHT TraversalFind the root (the first element in preorder) … Read more

Validate Binary Search Tree (C# and Python3)

Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key.The right subtree of a node contains only nodes with keys greater than the node’s key.Both the left and right subtrees must also be binary … Read more

Convert Sorted Array to Binary Search Tree (C# and Python3)

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one. Solution Step 1: select a root (the middle) Step 2: create subarrays at the left and the … Read more