144. Binary Tree Preorder Traversal

144. Binary Tree Preorder Traversal Given the root of a binary tree, return the preorder traversal of its nodes’ values. Example 1: Input: root = [1,null,2,3] Output: [1,2,3] Example 2: Input: root = [] Output: [] Example 3: Input: root = [1] Output: [1] Example 4: Input: root = [1,2] Output: [1,2] Example 5: Input: root = [1,null,2] Output: [1,2] ...

March 16, 2021 · 2 min · volyx

149. Max Points on a Line

149. Max Points on a Line Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line. Example 1: Input: points = [[1,1],[2,2],[3,3]] Output: 3 Example 2: Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] Output: 4 Constraints: 1 <= points.length <= 300 points[i].length == 2 -104 <= xi, yi <= 104 All the points are unique. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode sortedArrayToBST(int[] nums) { if (nums.length == 0) return null; return buildBST(nums, 0, nums.length - 1); } TreeNode buildBST(int[] nums, int lo, int hi) { if (hi < lo) { return null; } TreeNode node = new TreeNode(); int mid = lo + (hi - lo) / 2; node.val = nums[mid]; node.left = buildBST(nums, lo, mid - 1); node.right = buildBST(nums, mid + 1, hi); return node; } }

March 16, 2021 · 1 min · volyx

773. Sliding Puzzle

![https://leetcode.com/problems/sliding-puzzle/] On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0. A move consists of choosing 0 and a 4-directionally adjacent number and swapping it. The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]]. Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1. ...

February 7, 2021 · 4 min · volyx

Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). Example: Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC" Note: If there is no such window in S that covers all characters in T, return the empty string “”. If there is such window, you are guaranteed that there will always be only one unique minimum window in S. Solution: ...

May 31, 2020 · 1 min · volyx

Binary Tree Maximum Path Sum

Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. Example 1: Input: [1,2,3] 1 / \ 2 3 Output: 6 Example 2: ...

May 13, 2020 · 2 min · volyx