├── .gitignore ├── algorithms ├── 226.Invert Binary Tree │ ├── README.md │ └── Invert Binary Tree.js ├── 697.Degree of an Array │ ├── Degree of an Array.js │ └── README.md ├── 100.Same Tree │ ├── README.md │ └── Same tree.js ├── 229.Majority Element II │ ├── README.md │ └── Majority Element II.js ├── 103.Binary Tree Zigzag Level Order Traversal │ ├── README.md │ └── Binary Tree Zigzag Level Order Traversal.js ├── 344.Reverse String │ ├── README.md │ └── Reverse String.js ├── 104.Maximum Depth of Binary Tree │ ├── README.md │ └── Maximum Depth of Binary Tree.js ├── 111.Minimum Depth of Binary Tree │ ├── README.md │ └── Minimum Depth of Binary Tree.js ├── 169.Majority Element │ ├── README.md │ └── Majority Element.js ├── 236.Lowest Common Ancestor of a Binary Tree │ ├── README.md │ └── Lowest Common Ancestor of a Binary Tree.js ├── 110.Balanced Binary Tree │ ├── README.md │ └── Balanced Binary Tree.js ├── 235.Lowest Common Ancestor of a Binary Search Tree │ ├── README.md │ └── Lowest Common Ancestor of a Binary Search Tree.js ├── 108.Convert Sorted Array to Binary Search Tree │ ├── README.md │ └── Convert Sorted Array to Binary Search Tree.js ├── 217.Contains Duplicate │ ├── README.md │ └── Contains Duplicate.js ├── 219.Contains Duplicate II │ ├── README.md │ └── Contains Duplicate II.js ├── 34.Search for a Range │ ├── README.md │ └── Search for a Range.js ├── 5.Longest Palindromic Substring │ ├── Longest Palindromic Substring.js │ └── README.md ├── 101.Symmetric Tree │ ├── README.md │ └── Symmetric Tree.js ├── 119.Pascal's Triangle II │ ├── README.md │ └── Pascal's Triangle II.js ├── 225.Implement Stack using Queues │ ├── README.md │ └── Implement Stack using Queues.js ├── 142.Linked List Cycle II │ ├── README.md │ └── Linked List Cycle II.js ├── 209.Minimum Size Subarray Sum │ ├── README.md │ └── Minimum Size Subarray Sum.js ├── 222.Count Complete Tree Nodes │ ├── README.md │ └── Count Complete Tree Nodes.js ├── 232.Implement Queue using Stacks │ ├── README.md │ └── Implement Queue using Stacks.js ├── 105.Construct Binary Tree from Preorder and Inorder Traversal │ ├── README.md │ └── Construct Binary Tree from Preorder and Inorder Traversal.js ├── 173.Binary Search Tree Iterator │ ├── README.md │ └── Binary Search Tree Iterator.js ├── 94.Binary Tree Inorder Traversal │ ├── README.md │ └── Binary Tree Inorder Traversal.js ├── 206.Reverse Linked List │ ├── README.md │ └── Reverse Linked List.js ├── 268.Missing Number │ ├── README.md │ └── Missing Number.js ├── 106.Construct Binary Tree from Inorder and Postorder Traversal │ ├── README.md │ └── Construct Binary Tree from Inorder and Postorder Traversal.js ├── 99.Recover Binary Search Tree │ ├── README.md │ └── Recover Binary Search Tree.js ├── 144.Binary Tree Preorder Traversal │ ├── README.md │ └── Binary Tree Preorder Traversal.js ├── 145.Binary Tree Postorder Traversal │ ├── README.md │ └── Binary Tree Postorder Traversal.js ├── 283.Move Zeroes │ ├── README.md │ └── Move Zeroes.js ├── 98.Validate Binary Search Tree │ ├── README.md │ └── Validate Binary Search Tree.js ├── 21.Merge Two Sorted Lists │ ├── README.md │ └── Merge Two Sorted Lists.js ├── 404.Sum of Left Leaves │ ├── README.md │ └── Sum of Left Leaves.js ├── 83.Remove Duplicates from Sorted List │ ├── README.md │ └── Remove Duplicates from Sorted List.js ├── 118.Pascal's Triangle │ ├── README.md │ └── Pascal's Triangle.js ├── 153.Find Minimum in Rotated Sorted Array │ ├── README.md │ └── Find Minimum in Rotated Sorted Array.js ├── 349.Intersection of Two Arrays │ ├── README.md │ └── Intersection of Two Arrays.js ├── 9. Palindrome Number │ ├── README.md │ └── Palindrome Number.js ├── 230.Kth Smallest Element in a BST │ ├── README.md │ └── Kth Smallest Element in a BST.js ├── 27.Remove Element │ ├── README.md │ └── Remove Element.js ├── 33.Search in Rotated Sorted Array │ ├── README.md │ └── Search in Rotated Sorted Array.js ├── 92.Reverse Linked List II │ ├── README.md │ └── Reverse Linked List II.js ├── 257.Binary Tree Paths │ ├── README.md │ └── Binary Tree Paths.js ├── 107.Binary Tree Level Order Traversal II │ ├── README.md │ └── Binary Tree Level Order Traversal II.js ├── 1.Two Sum │ ├── README.md │ └── Two Sum.js ├── 19.Remove Nth Node From End of List │ ├── README.md │ └── Remove Nth Node From End of List.js ├── 26.Remove Duplicates from Sorted Array │ ├── README.md │ └── 26.Remove Duplicates from Sorted Array.js ├── 102.Binary Tree Level Order Traversal │ ├── README.md │ └── Binary Tree Level Order Traversal.js ├── 238.Product of Array Except Self │ ├── README.md │ └── Product of Array Except Self.js ├── 287.Find the Duplicate Number │ ├── README.md │ └── Find the Duplicate Number.js ├── 54.Spiral Matrix │ ├── README.md │ └── Spiral Matrix.js ├── 637.Average of Levels in Binary Tree │ ├── README.md │ └── Average of Levels in Binary Tree.js ├── 35.Search Insert Position │ ├── README.md │ └── Search Insert Position.js ├── 124.Binary Tree Maximum Path Sum │ ├── README.md │ └── Binary Tree Maximum Path Sum.js ├── 78.Subsets │ ├── README.md │ └── Subsets.js ├── 199.Binary Tree Right Side View │ ├── README.md │ └── Binary Tree Right Side View.js ├── 203.Remove Linked List Elements │ ├── README.md │ └── Remove Linked List Elements.js ├── 515.Find Largest Value in Each Tree Row │ ├── README.md │ └── Find Largest Value in Each Tree Row.js ├── 61.Rotate List │ ├── README.md │ └── Rotate List.js ├── 234.Palindrome Linked List │ ├── README.md │ └── Palindrome Linked List.js ├── 643.Maximum Average Subarray I │ ├── README.md │ └── Maximum Average Subarray I.js ├── 74.Search a 2D Matrix │ ├── README.md │ └── Search a 2D Matrix.js ├── 442.Find All Duplicates in an Array │ ├── README.md │ └── Find All Duplicates in an Array.js ├── 476.Number Complement │ ├── Number Complement.js │ └── README.md ├── 129.Sum Root to Leaf Numbers │ ├── README.md │ └── Sum Root to Leaf Numbers.js ├── 154.Find Minimum in Rotated Sorted Array II │ ├── README.md │ └── Find Minimum in Rotated Sorted Array II.js ├── 328.Odd Even Linked List │ ├── README.md │ └── Odd Even Linked List.js ├── 561.Array Partition I │ ├── README.md │ └── Array Partition I.js ├── 449.Serialize and Deserialize BST │ ├── README.md │ └── Serialize and Deserialize BST.js ├── 448.Find All Numbers Disappeared in an Array │ ├── README.md │ └── Find All Numbers Disappeared in an Array.js ├── 543.Diameter of Binary Tree │ ├── README.md │ └── Diameter of Binary Tree.js ├── 112.Path Sum │ ├── README.md │ └── Path Sum.js ├── 501.Find Mode in Binary Search Tree │ ├── README.md │ └── Find Mode in Binary Search Tree.js ├── 167.Two Sum II - Input array is sorted │ ├── README.md │ └── Two Sum II - Input array is sorted.js ├── 95.Unique Binary Search Trees II │ ├── README.md │ └── Unique Binary Search Trees II.js ├── 378.Kth Smallest Element in a Sorted Matrix │ ├── README.md │ └── Kth Smallest Element in a Sorted Matrix.js ├── 113.Path Sum II │ ├── README.md │ └── Path Sum II.js ├── 96.Unique Binary Search Trees │ ├── Unique Binary Search Trees.js │ └── README.md ├── 461.Hamming Distance │ ├── Hamming Distance.js │ └── README.md ├── 563.Binary Tree Tilt │ ├── README.md │ └── Binary Tree Tilt.js ├── 240.Search a 2D Matrix II │ ├── README.md │ └── Search a 2D Matrix II.js ├── 654.Maximum Binary Tree │ ├── README.md │ └── Maximum Binary Tree.js ├── 508.Most Frequent Subtree Sum │ ├── README.md │ └── Most Frequent Subtree Sum.js ├── 653.Two Sum IV - Input is a BST │ ├── README.md │ └── Two Sum IV - Input is a BST.js ├── 141.Linked List Cycle │ ├── README.md │ └── Linked List Cycle.js ├── 513.Find Bottom Left Tree Value │ ├── README.md │ └── Find Bottom Left Tree Value.js ├── 606. Construct String from Binary Tree │ ├── README.md │ └── Construct String from Binary Tree.js ├── 674.Longest Continuous Increasing Subsequence │ ├── README.md │ └── Longest Continuous Increasing Subsequence.js ├── 652.Find Duplicate Subtrees │ ├── README.md │ └── Find Duplicate Subtrees.js ├── 155.Min Stack │ ├── READMD.md │ └── Min Stack.js ├── 114.Flatten Binary Tree to Linked List │ ├── README.md │ └── Flatten Binary Tree to Linked List.js ├── 237.Delete Node in a Linked List │ ├── 237.Delete Node in a Linked List.js │ └── README.md ├── 2.Add Two Numbers │ ├── README.md │ └── Add Two Numbers.js ├── 437.Path Sum III │ ├── README.md │ └── Path Sum III.js ├── 117.Populating Next Right Pointers in Each Node II │ ├── README.md │ └── Populating Next Right Pointers in Each Node II.js ├── 669.Trim a Binary Search Tree │ ├── README.md │ └── Trim a Binary Search Tree.js ├── 297.Serialize and Deserialize Binary Tree │ ├── README.md │ └── Serialize and Deserialize Binary Tree.js ├── 572.Subtree of Another Tree │ ├── README.md │ └── Subtree of Another Tree.js ├── 538.Convert BST to Greater Tree │ ├── README.md │ └── Convert BST to Greater Tree.js ├── 7.Reverse Integer │ ├── Reverse Integer.js │ └── README.md ├── 671.Second Minimum Node In a Binary Tree │ ├── README.md │ └── Second Minimum Node In a Binary Tree.js ├── 445.Add Two Numbers II │ ├── README.md │ └── Add Two Numbers II.js ├── 617.Merge Two Binary Trees │ ├── README.md │ └── Merge Two Binary Trees.js ├── 450.Delete Node in a BST │ ├── README.md │ └── Delete Node in a BST.js ├── 116. Populating Next Right Pointers in Each Node │ ├── README.md │ └── Populating Next Right Pointers in Each Node.js ├── 4.Median of Two Sorted Arrays │ ├── README.md │ └── Median of Two Sorted Arrays.js ├── 623.Add One Row to Tree │ ├── README.md │ └── Add One Row to Tree.js ├── 160.Intersection of Two Linked Lists │ ├── README.md │ └── Intersection of Two Linked Lists.js ├── 655.Print Binary Tree │ ├── README.md │ └── Print Binary Tree.js ├── 662.Maximum Width of Binary Tree │ ├── Maximum Width of Binary Tree.js │ └── README.md └── 496.Next Greater Element I │ └── README.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.idea 2 | *.DS_Store 3 | -------------------------------------------------------------------------------- /algorithms/226.Invert Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Invert Binary Tree 2 | 3 | 翻转二叉树 -------------------------------------------------------------------------------- /algorithms/697.Degree of an Array/Degree of an Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by lei.wang on 2017/12/10. 3 | */ 4 | -------------------------------------------------------------------------------- /algorithms/100.Same Tree/README.md: -------------------------------------------------------------------------------- 1 | # Same Tree 2 | 3 | 给定两个二叉树,编写一个函数检查两个二叉树是否相等。 4 | 5 | 如果两个二叉树结构相等并且节点值也相等,则认为二叉树是相等的。 -------------------------------------------------------------------------------- /algorithms/229.Majority Element II/README.md: -------------------------------------------------------------------------------- 1 | # Majority Element II 2 | 3 | 给定一个整数n,寻找出现次数超过⌊ n/3 ⌋的数。算法应该有线性的时间复杂度和常量的存储空间。 4 | -------------------------------------------------------------------------------- /algorithms/103.Binary Tree Zigzag Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Zigzag Level Order Traversal 2 | 3 | 给定一个二叉树,返回节点的之字形遍历。 -------------------------------------------------------------------------------- /algorithms/344.Reverse String/README.md: -------------------------------------------------------------------------------- 1 | # Reverse String 2 | 3 | 写一个函数能将输入的字符串翻转返回。 4 | 5 | 例如: 6 | 7 | 给定字符串s = "hello", 返回 "olleh" 8 | -------------------------------------------------------------------------------- /algorithms/104.Maximum Depth of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Maximum Depth of Binary Tree 2 | 3 | 给定一个二叉树,查找其最大高度。 4 | 5 | 最大高度的定义是: 从根节点到最远叶子节点的节点数。 -------------------------------------------------------------------------------- /algorithms/111.Minimum Depth of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Minimum Depth of Binary Tree 2 | 3 | 给定一个二叉树,寻找其最小深度。 4 | 5 | 最小深度的定义: 从根节点到叶子节点节点路径的最小值。 -------------------------------------------------------------------------------- /algorithms/169.Majority Element/README.md: -------------------------------------------------------------------------------- 1 | # Majority Element 2 | 3 | 给定一个长度为n的数组,查找其中的多数元素,主要元素是指出现次数超过⌊ n/2 ⌋的元素。 4 | 5 | 你可以假设数组是非空的并且一定存在多数元素。 -------------------------------------------------------------------------------- /algorithms/236.Lowest Common Ancestor of a Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Lowest Common Ancestor of a Binary Tree 2 | 3 | 给定一个二叉树,查找树中给定两个节点的最小祖先节点。 4 | -------------------------------------------------------------------------------- /algorithms/110.Balanced Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Balanced Binary Tree 2 | 3 | 给定一个二叉树,确定它是否是平衡的。 4 | 5 | 对于这个问题,如果一个二叉树,每一个节点两个数的深度之差不大于1,则称其为平衡二叉树。 6 | -------------------------------------------------------------------------------- /algorithms/235.Lowest Common Ancestor of a Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Lowest Common Ancestor of a Binary Search Tree 2 | 3 | 给定一个二叉搜索顺,给到给定节点的最小公共祖先节点。 -------------------------------------------------------------------------------- /algorithms/108.Convert Sorted Array to Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Convert Sorted Array to Binary Search Tree 2 | 3 | 给定一个数组,其中的元素升序排列。将其转化成平衡二叉树。 4 | 5 | -------------------------------------------------------------------------------- /algorithms/217.Contains Duplicate/README.md: -------------------------------------------------------------------------------- 1 | # Contains Duplicate 2 | 3 | 给定一个数组的整数,找到其中包含的所有重复的数值。如果数组中的数值至少出现了两次,你的函数应该返回`true`,否则每个数值都是唯一的,返回`false`。 4 | 5 | -------------------------------------------------------------------------------- /algorithms/219.Contains Duplicate II/README.md: -------------------------------------------------------------------------------- 1 | # Contains Duplicate II 2 | 3 | 给定一个包含整数的数组 nums 并且和数字k, 寻找其中是否有两个不同的下标值 i 和 j,使得 nums[i] = nums[j], 并且 i 和 j 的差值为 k。 -------------------------------------------------------------------------------- /algorithms/34.Search for a Range/README.md: -------------------------------------------------------------------------------- 1 | # Search for a Range 2 | 3 | 给定一个升序排列的整数数组,寻找给定值的起始和终止位置。 4 | 5 | 你的算法运行时间复杂度必须为O(log n)。 6 | 7 | 如果没有在数组中找到目标数,返回[-1,-1]。 -------------------------------------------------------------------------------- /algorithms/5.Longest Palindromic Substring/Longest Palindromic Substring.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {string} 4 | */ 5 | var longestPalindrome = function(s) { 6 | 7 | }; -------------------------------------------------------------------------------- /algorithms/101.Symmetric Tree/README.md: -------------------------------------------------------------------------------- 1 | # Symmetric Tree 2 | 3 | 给定一个二叉树,检查其是否是镜像(关于中心对称)。 4 | 5 | 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 6 | 7 | 但二叉树 [1,2,2,null,3,null,3] 是非对称的。 8 | 9 | 注意: 以递归或者迭代的方式实现。 -------------------------------------------------------------------------------- /algorithms/119.Pascal's Triangle II/README.md: -------------------------------------------------------------------------------- 1 | # Pascal's Triangle II 2 | 3 | 给定索引值K, 返回帕斯卡三角形第K行的数据。 4 | 5 | 例如,给定值K为3, 6 | 7 | 返回 [1,3,3,1]。 8 | 9 | 注意: 10 | 11 | 你可以优化你的算法使得仅使用O(k)的额外空间吗? -------------------------------------------------------------------------------- /algorithms/225.Implement Stack using Queues/README.md: -------------------------------------------------------------------------------- 1 | # Implement Stack using Queues 2 | 3 | 使用队列实现下列栈的功能 4 | 5 | push(x) -- 将元素x移入栈中 6 | pop() -- 将栈顶端的元素移除 7 | top() -- 获得栈顶的元素 8 | empty() -- 栈是否为空 -------------------------------------------------------------------------------- /algorithms/142.Linked List Cycle II/README.md: -------------------------------------------------------------------------------- 1 | # Linked List Cycle II 2 | 3 | 给定一个链表,返回一个其中圆环开始的位置。如果没有圆环,返回null。 4 | 5 | 注意: 不要修改链表本身。 6 | 7 | 提示: 8 | 9 | 你能在不使用额外空间的条件下解决该问题吗? 10 | 11 | 12 | -------------------------------------------------------------------------------- /algorithms/209.Minimum Size Subarray Sum/README.md: -------------------------------------------------------------------------------- 1 | # Minimum Size Subarray Sum 2 | 3 | 给定有n个正整数的数组和一个整数s, 找到最小长度的连续子数组,使得子数组的和的值 sum >= s,如果不存在,则返回0。 4 | 5 | 6 | 例如,给定数组[2,3,1,2,4,3]和值7,满足条目要求的情况下,子数组为: [4,3] []() -------------------------------------------------------------------------------- /algorithms/222.Count Complete Tree Nodes/README.md: -------------------------------------------------------------------------------- 1 | # Count Complete Tree Nodes 2 | 3 | 给定一棵完整二叉树,计算节点数。 4 | 5 | 根据维基百科,完整二叉树的定义为: 6 | 7 | 在完整二叉树的每一行中,除了最后的元素,其他元素必须完全填满,并且在最后一行的所有元素都需要尽可能的靠左。最后一行的(h)节点数可以是1到2h。 8 | -------------------------------------------------------------------------------- /algorithms/232.Implement Queue using Stacks/README.md: -------------------------------------------------------------------------------- 1 | # Implement Queue using Stacks 2 | 3 | 使用栈来模拟队列中的下列方法: 4 | 5 | - push(x) -- 将元素x推入队列尾 6 | - pop() -- 从队列前部移除元素 7 | - peek() -- 获得元素前部元素 8 | - empty() -- 返回数据是否为空 -------------------------------------------------------------------------------- /algorithms/105.Construct Binary Tree from Preorder and Inorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Construct Binary Tree from Preorder and Inorder Traversal 2 | 3 | 给定一棵二叉树的先序遍历和中序遍历,构造这棵二叉树。 4 | 5 | 注意: 6 | 7 | 你可以假设树中不存在重复的元素。 8 | -------------------------------------------------------------------------------- /algorithms/173.Binary Search Tree Iterator/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search Tree Iterator 2 | 3 | 实现二叉树的迭代器,你的迭代器将使用二叉搜索树的根节点初始化。 4 | 5 | 调用next()函数将返回在二叉搜索树中接下来最小的值。 6 | 7 | 注意:next()和hasNext()必须以常量的速度运行并使用O(h)的存储空间,其中h是树高。 -------------------------------------------------------------------------------- /algorithms/344.Reverse String/Reverse String.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {string} 4 | */ 5 | var reverseString = function(s) { 6 | return Array.prototype.reverse.apply(s.split('')).join(''); 7 | }; -------------------------------------------------------------------------------- /algorithms/94.Binary Tree Inorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Inorder Traversal 2 | 3 | 给定一个二叉树,返回中序遍历的结果值。 4 | 5 | 例如: 6 | 7 | 给定二叉树: [1,null,2,3], 8 | 9 | 返回 [1,3,2] 10 | 11 | 注意:递归版本是非常简单的,你能给出迭代版本吗? -------------------------------------------------------------------------------- /algorithms/206.Reverse Linked List/README.md: -------------------------------------------------------------------------------- 1 | # 206. Reverse Linked List 2 | 3 | Reverse a singly linked list. 4 | 5 | Hint: 6 | 7 | A linked list can be reversed either iteratively or recursively. Could you implement both? -------------------------------------------------------------------------------- /algorithms/268.Missing Number/README.md: -------------------------------------------------------------------------------- 1 | # Missing Number 2 | 3 | 给定一个数组其中包含n个不相同的数,从0到n,找到数组中缺失的数。 4 | 5 | 例如: 6 | 7 | 给定数组 num = [0, 1, 3], 返回 2 8 | 9 | 注意: 10 | 11 | 你的算法的时间复杂度应该是线性的,你可以使用常量的存储空间实现吗? 12 | 13 | -------------------------------------------------------------------------------- /algorithms/106.Construct Binary Tree from Inorder and Postorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Construct Binary Tree from Inorder and Postorder Traversal 2 | 3 | 给定一颗二叉树的中序遍历和后序遍历,构造这棵二叉树。 4 | 5 | 注意: 6 | 7 | 你可以假设这棵树中不存在重复的节点。 8 | 9 | -------------------------------------------------------------------------------- /algorithms/99.Recover Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Recover Binary Search Tree 2 | 3 | 二叉搜索树的两个元素被错误的交换了。 4 | 5 | 复原该二叉树但不要改变它的结构。 6 | 7 | 8 | 注意: 9 | 10 | 使用空间复杂度为O(n)的解决方案相对比较简单,你是否可以在常量的空间内使用。 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /algorithms/144.Binary Tree Preorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Preorder Traversal 2 | 3 | 给定一个二叉树,返回树节点的先序遍历。 4 | 5 | 例如: 6 | 7 | 给定二叉树: {1,#,2,3} 8 | 9 | 返回 [1,2,3] 10 | 11 | 注意: 12 | 13 | 递归解法非常简单,你能用迭代法实现吗? 14 | -------------------------------------------------------------------------------- /algorithms/145.Binary Tree Postorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Postorder Traversal 2 | 3 | 给定一个二叉树,返回树节点的先序遍历。 4 | 5 | 例如: 6 | 7 | 给定二叉树: {1,#,2,3} 8 | 9 | 返回 [3,2,1] 10 | 11 | 注意: 12 | 13 | 递归解法非常简单,你能用迭代法实现吗? 14 | -------------------------------------------------------------------------------- /algorithms/283.Move Zeroes/README.md: -------------------------------------------------------------------------------- 1 | # Move Zeroes 2 | 3 | 给定一个数组nums, 写一个函数将所有的0移到最后并保持所有非零元素的次序。 4 | 5 | 例如: 给定数组 nums = [0, 1, 0, 3, 12], 调用函数之后应该是 [1, 3, 12, 0, 0]。 6 | 7 | 注意: 8 | 9 | 必须就地转化不要额外复制数组。 10 | 11 | 最小化操作的次数。 -------------------------------------------------------------------------------- /algorithms/98.Validate Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Validate Binary Search Tree 2 | 3 | 给定一颗二叉树,确定它是否是一颗有效的二叉搜索树(BST) 4 | 5 | 二叉搜索树的定义如下: 6 | 7 | - 节点的左子树包含的节点值小于节点的值 8 | - 节点的右子树包含的节点值大于节点的值 9 | - 左子树和右子树的值必须是二叉搜索树 10 | -------------------------------------------------------------------------------- /algorithms/21.Merge Two Sorted Lists/README.md: -------------------------------------------------------------------------------- 1 | # 21. Merge Two Sorted Lists 2 | 3 | Merge two sorted linked lists and return it as a new list. 4 | The new list should be made by splicing together the nodes of the first two lists. 5 | 6 | -------------------------------------------------------------------------------- /algorithms/404.Sum of Left Leaves/README.md: -------------------------------------------------------------------------------- 1 | # Sum of Left Leaves 2 | 3 | 寻找给定二叉树的所有左叶子节点的和。 4 | 5 | 例如: 6 | 7 | ``` 8 | 3 9 | / \ 10 | 9 20 11 | / \ 12 | 15 7 13 | 14 | 二叉树中有两个左叶子节点,其值分别是9和15,返回24 15 | ``` 16 | -------------------------------------------------------------------------------- /algorithms/83.Remove Duplicates from Sorted List/README.md: -------------------------------------------------------------------------------- 1 | Given a sorted linked list, delete all duplicates such that each element appear only once. 2 | 3 | For example, 4 | Given 1->1->2, return 1->2. 5 | Given 1->1->2->3->3, return 1->2->3. -------------------------------------------------------------------------------- /algorithms/118.Pascal's Triangle/README.md: -------------------------------------------------------------------------------- 1 | # Pascal's Triangle 2 | 3 | 给定行数,产生一个帕斯卡三角形。 4 | 5 | 例如: 给定数字5 6 | 7 | 返回 8 | 9 | ``` 10 | [ 11 | [1], 12 | [1,1], 13 | [1,2,1], 14 | [1,3,3,1], 15 | [1,4,6,4,1] 16 | ] 17 | ``` -------------------------------------------------------------------------------- /algorithms/153.Find Minimum in Rotated Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | # Find Minimum in Rotated Sorted Array 2 | 3 | 假设你事先知道以升序排列的数组在某个枢纽处被旋转。 4 | 5 | 例如: 6 | 7 | `0 1 2 4 5 6 7` 变为 `4 5 6 7 0 1 2` 8 | 9 | 找到节点的最小值。 10 | 11 | 你可以假设数组中不含有重复的数字。 -------------------------------------------------------------------------------- /algorithms/349.Intersection of Two Arrays/README.md: -------------------------------------------------------------------------------- 1 | # Intersection of Two Arrays 2 | 3 | 给定两个数组,编写一个函数计算他们的交叉点。 4 | 5 | 例如: 6 | 7 | 给定数组 num1 = [1, 2, 2, 1], num2 = [2, 2], 返回 [2]。 8 | 9 | 注意: 10 | 11 | - 结果中的每个元素必须唯一 12 | - 结果不对次序做要求 -------------------------------------------------------------------------------- /algorithms/9. Palindrome Number/README.md: -------------------------------------------------------------------------------- 1 | # Palindrome Number 2 | 3 | 确定一个整数是否是回文数,不要使用额外的内存空间。 4 | 5 | ## 思路 6 | 7 |   如果判断一个回文数,最简单的方式就是把一个数翻转过来判断与之前的数是否相等,如果相等,说明是回文数,如果不是,则不是回文数。 8 | 9 |   例如: 121翻转过来是121,两者相等说明是回文数。123翻转过来是321,两者不相等说明不是回文数。 -------------------------------------------------------------------------------- /algorithms/230.Kth Smallest Element in a BST/README.md: -------------------------------------------------------------------------------- 1 | # Kth Smallest Element in a BST 2 | 3 | 给定一个二叉搜索树,编写函数kthSmallest来查找树中第K小的元素。 4 | 5 | 注意: 6 | 7 | 你可以假设K的值总是有效的。 8 | 9 | 再接在励: 10 | 11 | 如果这颗二叉搜索树经常改变(插入或删除节点操作),你需要怎样查找第K小的节点?如果进行优化。 -------------------------------------------------------------------------------- /algorithms/27.Remove Element/README.md: -------------------------------------------------------------------------------- 1 | # Remove Element 2 | 3 | 给定一个数组和一个值,移除数组中该值的实例并返回新的值。 4 | 5 | 不要分配数组分配额外的空间,你必须在常量空间中完成。 6 | 7 | 元素的次序可以改变,不必考虑新的数组长度之外数组的值。 8 | 9 | 例如: 10 | 11 | 给定数组[3,2,2,3],值为3 12 | 13 | 你的函数应该返回长度为2,前两个元素值为2。 -------------------------------------------------------------------------------- /algorithms/33.Search in Rotated Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | # Search in Rotated Sorted Array 2 | 3 | 假设已知升序排序的数组在某个节点被旋转。 4 | 5 | 例如: 0 1 2 4 5 6 7 可能变成了 4 5 6 7 0 1 2 6 | 7 | 给定你一个值在数组中搜索,如果在数组中存在则返回对应下标,否则返回-1。 8 | 9 | 你可以假设数组中不存在重复的数。 10 | -------------------------------------------------------------------------------- /algorithms/92.Reverse Linked List II/README.md: -------------------------------------------------------------------------------- 1 | # Reverse Linked List II 2 | 3 | 翻转位置m到n的链表,程序要求原地单次执行。 4 | 5 | 例如: 6 | 7 | 给定 1->2->3->4->5->NULL, m = 2 , n = 4. 8 | 9 | 返回 1->4->3->2->5->NULL 10 | 11 | 注意: 12 | 13 | m, n 满足下面条件: 1 ≤ m ≤ n ≤ 链表长度 -------------------------------------------------------------------------------- /algorithms/257.Binary Tree Paths/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Paths 2 | 3 | 给定一棵二叉树,返回所有的根节点到叶子节点的路径。 4 | 5 | 例如给定下面的二叉树: 6 | 7 | ``` 8 | 1 9 | / \ 10 | 2 3 11 | \ 12 | 5 13 | ``` 14 | 15 | 所有的路径为: 16 | 17 | ``` 18 | ["1->2->5", "1->3"] 19 | ``` -------------------------------------------------------------------------------- /algorithms/107.Binary Tree Level Order Traversal II/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Level Order Traversal II 2 | 3 | 给定一个二叉树,从底至顶遍历树的节点,并返回节点。 4 | 5 | 例如: 6 | 7 | 给定二叉树: [3,9,20,null,null,15,7] 8 | 9 | 返回节点: 10 | 11 | [ 12 | [15,7], 13 | [9,20], 14 | [3] 15 | ] -------------------------------------------------------------------------------- /algorithms/1.Two Sum/README.md: -------------------------------------------------------------------------------- 1 | # Two Sum 2 | 3 | 给定一个整数数组,返回两个数的位置下标使得两个数之和等于特定的书。 4 | 5 | 你可以假设每个输入仅有一个答案,并且你不可以使用同一个元素两次。 6 | 7 | 例如: 8 | 9 | > nums: [2, 7, 11, 15], 目标数值: 9 10 | 11 | 12 | 因为num[0] + nums[1] = 2 + 7 = 9; 13 | 14 | 返回 [0, 1]; 15 | 16 | 17 | -------------------------------------------------------------------------------- /algorithms/19.Remove Nth Node From End of List/README.md: -------------------------------------------------------------------------------- 1 | # Remove Nth Node From End of List 2 | 3 | 给定一个链表,移除从结尾数的第N个节点,并返回头结点。 4 | 5 | 例如: 6 | 7 | > 给定链表: 1->2->3->4->5, 并且 n = 2 8 | > 9 | > 当移除了倒数第二个节点,链表变为: 1->2->3->5。 10 | 11 | 注意: 12 | 13 | 给定的n值总是有效的,并且尝试一次遍历解决。 -------------------------------------------------------------------------------- /algorithms/26.Remove Duplicates from Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | # 26. Remove Duplicates from Sorted Array 2 | 3 | 给定一个排序数组,移除重复项使得每个元素仅出现一次并返回新数组长度。 4 | 5 | 不要为数组分配额外的空间,你必须在常数空间内完成。 6 | 7 | 例如: 8 | 9 | 给定输入 num = [1,1,2], 10 | 11 | 你的函数应该返回长度为2,前两个元素分别是1,2。在数组长度之外的数不会被考虑。 12 | -------------------------------------------------------------------------------- /algorithms/102.Binary Tree Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Level Order Traversal 2 | 3 | 给定一个二叉树,返回树节点值的层次遍历结果(从左到右,逐次遍历) 4 | 5 | 例如: 6 | 7 | 给定二叉树: [3,9,20,null,null,15,7] 8 | 9 | 返回层次遍历的结果是: 10 | 11 | [ 12 | [3], 13 | [9,20], 14 | [15,7] 15 | ] 16 | 17 | -------------------------------------------------------------------------------- /algorithms/238.Product of Array Except Self/README.md: -------------------------------------------------------------------------------- 1 | # Product of Array Except Self 2 | 3 | 给定一个含有n个整数的数组 nums , 其中 n > 1.返回的数组结果 output 其中每个 output[i] 的值等于其他元素的乘积。 4 | 5 | 请不要使用除法并且在线性时间内解决。 6 | 7 | 例如,给定 [1,2,3,4], 返回 [24,12,8,6]。 8 | 9 | 坚持: 10 | 11 | 你可以使用常量的存储空间内完成吗? 12 | -------------------------------------------------------------------------------- /algorithms/287.Find the Duplicate Number/README.md: -------------------------------------------------------------------------------- 1 | # Find the Duplicate Number 2 | 3 | 给定一个数组,其中包含n+1 个数,数的范围在1~n之间,说明至少存在一个重复的数组。假设其中仅有一个重复数,寻找给重复的数字。 4 | 5 | 注意: 6 | 7 | 1. 你不能修改该数组,假设该数组仅是可读的。 8 | 2. 你必须使用常量的存储空间。 9 | 3. 你的运行时间复杂度必须小于O(n2)。 10 | 4. 数组中仅含有一个重复数,但可能重复多次。 11 | -------------------------------------------------------------------------------- /algorithms/54.Spiral Matrix/README.md: -------------------------------------------------------------------------------- 1 | # Spiral Matrix 2 | 3 | 给定一个 m * n 元素的矩阵,其中 m 是行数,n是列数,以螺旋形式返回矩阵的所有元素。 4 | 5 | 例如: 6 | 7 | 给定下列矩阵: 8 | 9 | >[ 10 | > 11 | > [ 1, 2, 3 ], 12 | > 13 | > [ 4, 5, 6 ], 14 | > 15 | > [ 7, 8, 9 ] 16 | > 17 | >] 18 | 19 | 你应该返回 [1,2,3,6,9,8,7,4,5] -------------------------------------------------------------------------------- /algorithms/637.Average of Levels in Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Average of Levels in Binary Tree 2 | 3 | 给定一个非空二叉树,以数组的形式返回每层树节点的平均值。 4 | 5 | 例如: 6 | 7 | ``` 8 | Input: 9 | 3 10 | / \ 11 | 9 20 12 | / \ 13 | 15 7 14 | Output: 15 | 16 | [3, 14.5, 11] 17 | ``` 18 | -------------------------------------------------------------------------------- /algorithms/35.Search Insert Position/README.md: -------------------------------------------------------------------------------- 1 | # Search Insert Position 2 | 3 | 给定一个排序好的数组和一个目标值,如果能找到该节点则返回对应的位置。否则,返回如果需要插入该序列的位置。 4 | 5 | 你可以假设数组中没有重复的数值。 6 | 7 | 例如: 8 | 9 | ``` 10 | [1,3,5,6], 5 → 2 11 | [1,3,5,6], 2 → 1 12 | [1,3,5,6], 7 → 4 13 | [1,3,5,6], 0 → 0 14 | ``` 15 | -------------------------------------------------------------------------------- /algorithms/124.Binary Tree Maximum Path Sum/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Maximum Path Sum 2 | 3 | 给定一颗二叉树,寻找最大路劲之和。 4 | 5 | 对于该问题,路径的定义为从树中的任意节点起沿着父子关系的一系列节点的和。路径至少要包含一个节点并且不需要经过根节点。 6 | 7 | 例如: 8 | 9 | 给定下面的二叉树: 10 | 11 | ``` 12 | 1 13 | / \ 14 | 2 3 15 | ``` 16 | 17 | 返回6 -------------------------------------------------------------------------------- /algorithms/78.Subsets/README.md: -------------------------------------------------------------------------------- 1 | # Subsets 2 | 3 | 给定一个整数数组,其实的元素都是不相同的。返回所有可能的子元素集合。 4 | 5 | 注意: 答案中不能包含重复的子元素集合。 6 | 7 | 例如: 8 | 9 | 如果 nums = [1,2,3], 答案为: 10 | 11 | [ 12 | [3], 13 | [1], 14 | [2], 15 | [1,2,3], 16 | [1,3], 17 | [2,3], 18 | [1,2], 19 | [] 20 | ] 21 | -------------------------------------------------------------------------------- /algorithms/199.Binary Tree Right Side View/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Right Side View 2 | 3 | 给定一颗二叉树 ,想象你站在二叉树的右边,返回你看到的从顶到低的节点。 4 | 5 | 例如,给定二叉树: 6 | 7 | ``` 8 | 1 <--- 9 | / \ 10 | 2 3 <--- 11 | \ \ 12 | 5 4 <--- 13 | ``` 14 | 15 | 16 | 你应该返回[1,3,4] -------------------------------------------------------------------------------- /algorithms/203.Remove Linked List Elements/README.md: -------------------------------------------------------------------------------- 1 | # 203. Remove Linked List Elements 2 | 3 | Remove all elements from a linked list of integers that have value val. 4 | 5 | Example: 6 | 7 | Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 8 | 9 | Return: 1 --> 2 --> 3 --> 4 --> 5 10 | -------------------------------------------------------------------------------- /algorithms/515.Find Largest Value in Each Tree Row/README.md: -------------------------------------------------------------------------------- 1 | # Find Largest Value in Each Tree Row 2 | 3 | 你需要查找二叉树每行中最大的节点。 4 | 5 | 例如: 6 | 7 | 8 | ``` 9 | 输入: 10 | 11 | 1 12 | / \ 13 | 3 2 14 | / \ \ 15 | 5 3 9 16 | 17 | 输出: [1, 3, 9] 18 | ``` -------------------------------------------------------------------------------- /algorithms/5.Longest Palindromic Substring/README.md: -------------------------------------------------------------------------------- 1 | # Longest Palindromic Substring 2 | 3 | 给定一个字符串s,寻找字符串s中最长的回文子串。你可以假设字符串最长长度为:1000。 4 | 5 | 6 | 例如: 7 | 8 | > Input: "babad" 9 | > 10 | > Output: "bab" 11 | > 12 | > Output: "bab" 13 | 14 | 例如: 15 | 16 | > Input: "cbbd" 17 | > 18 | > Output: "bb" -------------------------------------------------------------------------------- /algorithms/61.Rotate List/README.md: -------------------------------------------------------------------------------- 1 | # 61. Rotate List 2 | 3 | Given a list, rotate the list to the right by k places, where k is non-negative. 4 | 5 | For example: 6 | 7 | Given 1->2->3->4->5->NULL and k = 2, 8 | 9 | return 4->5->1->2->3->NULL. 10 | 11 | 思路:通过首先形成一个环,然后对环进行切割的方式实现 12 | 注意,题目要求的K是从右开始数的 -------------------------------------------------------------------------------- /algorithms/234.Palindrome Linked List/README.md: -------------------------------------------------------------------------------- 1 | # 234. Palindrome Linked List 2 | 3 | Given a singly linked list, determine if it is a palindrome. 4 | 5 | Follow up: 6 | 7 | Could you do it in O(n) time and O(1) space? 8 | 9 | 思路: 10 | 反转链表法,将链表后半段原地翻转,再将前半段、后半段依次比较,判断是否相等, 11 | 时间复杂度O(n),空间复杂度为O(1)满足题目要求。 -------------------------------------------------------------------------------- /algorithms/643.Maximum Average Subarray I/README.md: -------------------------------------------------------------------------------- 1 | # Maximum Average Subarray I 2 | 3 | 给定一个包含n个整数的数组,找到长度为k的连续子数组,使得其平均值最大,你需要输出最大的平均值。 4 | 5 | 例子1: 6 | 7 | ``` 8 | 输入: [1,12,-5,-6,50,3], k = 4 9 | 输出t: 12.75 10 | ``` 11 | 12 | 注意: 13 | 14 | 1. 1 <= k <= n <= 30,000. 15 | 2. 给定数组的范围在 [-10,000, 10,000] 之间。 -------------------------------------------------------------------------------- /algorithms/74.Search a 2D Matrix/README.md: -------------------------------------------------------------------------------- 1 | # Search a 2D Matrix 2 | 3 | 编写一个有效的算法能在 m x n 的矩阵中查找数值。矩阵有下面的特性: 4 | 5 | - 从左到右的整数是排序过的 6 | - 每行第一个整数总是大于前一个行的最后一个整数 7 | 8 | 例如: 9 | 10 | 考虑下面整数: 11 | 12 | [ 13 | [1, 3, 5, 7], 14 | [10, 11, 16, 20], 15 | [23, 30, 34, 50] 16 | ] 17 | 18 | 给定数值3,返回`true` -------------------------------------------------------------------------------- /algorithms/442.Find All Duplicates in an Array/README.md: -------------------------------------------------------------------------------- 1 | # Find All Duplicates in an Array 2 | 3 | 给定一个整数数组,其中有 1 ≤ a[i] ≤ n (n 是数组长度),部分元素出现两次而其他元素出现仅一次。 4 | 5 | 找到其中出现两个的元素。 6 | 7 | 你可以不使用额外的存储空间并在线性时间内解决吗? 8 | 9 | 例如: 10 | 11 | 输入: 12 | 13 | ``` 14 | [4,3,2,7,8,2,3,1] 15 | ``` 16 | 17 | 输出: 18 | 19 | ``` 20 | [2,3] 21 | ``` -------------------------------------------------------------------------------- /algorithms/476.Number Complement/Number Complement.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by MrErHu on 2017/2/3. 3 | */ 4 | 5 | /** 6 | * @param {number} num 7 | * @return {number} 8 | */ 9 | 10 | var findComplement = function(num) { 11 | var i = 1; 12 | while (i <= num){ 13 | i = i * 2; 14 | } 15 | return (i-1) ^ num; 16 | }; 17 | -------------------------------------------------------------------------------- /algorithms/129.Sum Root to Leaf Numbers/README.md: -------------------------------------------------------------------------------- 1 | # Sum Root to Leaf Numbers 2 | 3 | 4 | 给定一个二叉树,其中仅包含从0-9的整数,从根节点到叶子节点的每个路径对代表一个数值。 5 | 6 | 7 | 例如,从根节点到叶子节点的路径为1->2->3,代表数字123。 8 | 9 | 寻找所有根节点到叶子节点的路径和。 10 | 11 | 例如: 12 | 13 | ``` 14 | 1 15 | / \ 16 | 2 3 17 | ``` 18 | 19 | 路径1->2代表12。 20 | 21 | 路径1->3代表13。 22 | 23 | 返回的和为 12 + 13 = 25. -------------------------------------------------------------------------------- /algorithms/154.Find Minimum in Rotated Sorted Array II/README.md: -------------------------------------------------------------------------------- 1 | # Find Minimum in Rotated Sorted Array II 2 | 3 | > 接着上面的题: Find Minimum in Rotated Sorted Array, 4 | > 5 | > 如果允许数重复呢? 6 | > 7 | > 这会影响运行时的复杂度吗?为什么? 8 | 9 | 假设你事先知道以升序排列的数组在某个枢纽处被旋转。 10 | 11 | 例如: 12 | 13 | `0 1 2 4 5 6 7` 变为 `4 5 6 7 0 1 2` 14 | 15 | 找到节点的最小值。 16 | 17 | 数组中可能包含重复的数字。 -------------------------------------------------------------------------------- /algorithms/328.Odd Even Linked List/README.md: -------------------------------------------------------------------------------- 1 | # Odd Even Linked List 2 | 3 | 给定一个单链表,将所有的奇数节点分成一组,后面接着所有的偶数节点。请注意我们说的是节点序号而不是节点中的数值。 4 | 5 | 你应该就地解决,程序的空间复杂度为O(1),时间复杂度为:O(n)。 6 | 7 | 例如: 8 | > 输入:1->2->3->4->5->NULL 9 | > 10 | > 返回: 1->3->5->2->4->NULL 11 | 12 | 注意: 13 | 14 | 奇数分组和偶数分组的相对次序应该按照输入次序保持。 15 | 16 | 第一个节点被认为是奇数,第二个节点被认为是偶数。以此类推.. 17 | -------------------------------------------------------------------------------- /algorithms/561.Array Partition I/README.md: -------------------------------------------------------------------------------- 1 | # 561. Array Partition I 2 | 3 | 给一个有2n个整数的数组,你的任务是将这些整数分组成n对整数,例如(a1, b1), (a2, b2), ..., (an, bn),使得从0到n的过程中(ai, bi)中最小值的和尽可能的大。 4 | 5 | 例如: 6 | 7 | 输入: `[1,4,3,2]` 8 | 9 | 输出: 4 10 | 11 | 解释: n是2,最大值为: 4 = min(1, 2) + min(3, 4) 12 | 13 | 14 | 我的思路: 15 | 16 | 肯定是先排序,然后选择偶数序号的数的和。这样肯定是最大的。 17 | 18 | 19 | -------------------------------------------------------------------------------- /algorithms/449.Serialize and Deserialize BST/README.md: -------------------------------------------------------------------------------- 1 | # Serialize and Deserialize BST 2 | 3 | 序列化(Serialization)是一个将数据结构或者对象转化成比特序列的过程,使得其可以被存储在文件或者内存中,或者可以被网络传送,以便可以被别的计算机系统重建。 4 | 5 | 设计一套算法来序列化和反序列化二叉搜索树,并不限制你的序列化和反序列化算法应该怎么工作。你只需要保证二叉树可以被序列化成字符串并且字符串也可以被反序列化成原来的树结构。 6 | 7 | 编码的字符串应该尽可能的紧凑。 8 | 9 | 注意: 10 | 11 | 不要使用类、全局、静态的变量存储状态,你的序列化和重构算法应该是无状态的。 12 | -------------------------------------------------------------------------------- /algorithms/448.Find All Numbers Disappeared in an Array/README.md: -------------------------------------------------------------------------------- 1 | # Find All Numbers Disappeared in an Array 2 | 3 | 给定一个整数数组,其中数字值为1 ≤ a[i] ≤ n, 其中 n 是数组的长度,部分元素可能会出现两个,有些会出现一次。 4 | 5 | 寻找[1, n]中在数组中没有出现的元素。 6 | 7 | 你可以在不使用额外的存储空间和线性的时间解决吗? 你可以假设返回的列表不计做额外的存储空间。 8 | 9 | 例如: 10 | 11 | ``` 12 | Input: 13 | [4,3,2,7,8,2,3,1] 14 | 15 | Output: 16 | [5,6] 17 | ``` 18 | -------------------------------------------------------------------------------- /algorithms/543.Diameter of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Diameter of Binary Tree 2 | 3 | 给定一个二叉树,你需要来计算二叉树的直径。 4 | 5 | 二叉树的直径是指在二叉树中两个节点之间最长的距离。这条路径可能经过根节点,也可能不经过根节点。 6 | 7 | 例如: 8 | 9 | 给定二叉树: 10 | 11 | ``` 12 | 1 13 | / \ 14 | 2 3 15 | / \ 16 | 4 5 17 | ``` 18 | 19 | 返回3,是路径[4,2,1,3]或者[5,2,1,3]的长度。 20 | 21 | 注意: 路径的长度是指两个节点之间边的个数。 -------------------------------------------------------------------------------- /algorithms/27.Remove Element/Remove Element.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} val 4 | * @return {number} 5 | */ 6 | var removeElement = function (nums, val) { 7 | var pos = 0; 8 | for (let i = 0; i < nums.length; i++) { 9 | if(nums[i] !== val){ 10 | nums[pos++] = nums[i]; 11 | } 12 | } 13 | return pos; 14 | }; -------------------------------------------------------------------------------- /algorithms/697.Degree of an Array/README.md: -------------------------------------------------------------------------------- 1 | # Degree of an Array 2 | 3 | 给定一个非空的非负整数数组,数组的度定义是其中出现频次最高的元素。 4 | 5 | 你的任务是找出最小长度的子数组,使得其拥有相同的度。 6 | 7 | ``` 8 | Example 1: 9 | Input: [1, 2, 2, 3, 1] 10 | Output: 2 11 | ``` 12 | 13 | ``` 14 | Example 2: 15 | Input: [1,2,2,3,1,4,2] 16 | Output: 6 17 | ``` 18 | 19 | 注意: 20 | 21 | nums的长度是1到5000 22 | nums[i]是在0到4999之间 23 | 24 | -------------------------------------------------------------------------------- /algorithms/112.Path Sum/README.md: -------------------------------------------------------------------------------- 1 | # Path Sum 2 | 3 | 给定一个二叉树和一个值,确定二叉树中是否有一条从根节点到叶子节点的路径,这条路径上的节点值之和是给定的数。 4 | 5 | 例如: 6 | 7 | 给定下面的二叉树和数字22: 8 | 9 | 10 | ``` 11 | 5 12 | / \ 13 | 4 8 14 | / / \ 15 | 11 13 4 16 | / \ \ 17 | 7 2 1 18 | ``` 19 | 20 | 返回true,因为存在路径5->4->11->2,其和为22。 21 | -------------------------------------------------------------------------------- /algorithms/501.Find Mode in Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Find Mode in Binary Search Tree 2 | 3 | 给定一棵带有重复的二叉搜索树(BST),在给定的二叉搜索树中查找所有的mode(最常出现的元素)。 4 | 5 | 假设BST的定义如下: 6 | 7 | 节点的左子树包含的节点值都小于等于当前节点的值。 8 | 节点的右子树包含的节点值都大于等于当前节点的值。 9 | 左右子树都是二叉搜索树。 10 | 11 | 例如: 12 | 13 | 给定BST: [1,null,2,2] 14 | 15 | ``` 16 | 1 17 | \ 18 | 2 19 | / 20 | 2 21 | ``` 22 | 23 | 返回[2] -------------------------------------------------------------------------------- /algorithms/167.Two Sum II - Input array is sorted/README.md: -------------------------------------------------------------------------------- 1 | # Two Sum II - Input array is sorted 2 | 3 | 给定一个已经升序排列的数组,找出两个值使得二者相加等于特定的值。 4 | 5 | 函数twoSum应该返回两个数加起来等于给定的值的下标值,其中index1必须小于index2。请注意你返回的值不是基于0的位置。 6 | 7 | 你可以假设每个输入都有唯一的答案,你不能使用相同元素两次。 8 | 9 | 输入: 10 | 11 | ``` 12 | numbers={2, 7, 11, 15}, target=9 13 | ``` 14 | 15 | 输出: 16 | 17 | ``` 18 | Output: index1=1, index2=2 19 | ``` -------------------------------------------------------------------------------- /algorithms/561.Array Partition I/Array Partition I.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var arrayPairSum = function (nums) { 6 | 7 | nums.sort((a, b) => a - b) 8 | 9 | var rs = 0; 10 | 11 | nums.forEach((val, index) => { 12 | if(index % 2 !== 1){ 13 | rs += val; 14 | } 15 | }) 16 | 17 | return rs; 18 | 19 | }; -------------------------------------------------------------------------------- /algorithms/95.Unique Binary Search Trees II/README.md: -------------------------------------------------------------------------------- 1 | # Unique Binary Search Trees II 2 | 3 | 给定值n, 返回其中独一无二的二叉搜索树,其中存的值为1...n。 4 | 5 | 例如: 6 | 7 | 给定值3,你的程序应该返回下面的五个二叉树。 8 | 9 | ``` 10 | 1 3 3 2 1 11 | \ / / / \ \ 12 | 3 2 1 1 3 2 13 | / / \ \ 14 | 2 1 2 3 15 | ``` -------------------------------------------------------------------------------- /algorithms/378.Kth Smallest Element in a Sorted Matrix/README.md: -------------------------------------------------------------------------------- 1 | # Kth Smallest Element in a Sorted Matrix 2 | 3 | 给定一个 n x n 的矩阵,其中每行每列都已经递增排序,在矩阵中找到第k个小的元素。 4 | 5 | 注意是在排序数组中第k个元素,而不是第k个不同元素。 6 | 7 | 例如: 8 | 9 | ``` 10 | matrix = [ 11 | [ 1, 5, 9], 12 | [10, 11, 13], 13 | [12, 13, 15] 14 | ], 15 | k = 8, 16 | 17 | return 13. 18 | ``` 19 | 20 | 注意: 21 | 22 | 你可以假设k是有效的,即 1 ≤ k ≤ n ^ 2 23 | -------------------------------------------------------------------------------- /algorithms/113.Path Sum II/README.md: -------------------------------------------------------------------------------- 1 | # Path Sum II 2 | 3 | 给定一颗二叉树和一个值,寻找从根节点到叶子节点的路径,使得其路径之和等于给定的数值。 4 | 5 | 例如: 6 | 7 | 给定二叉树,和值22 8 | 9 | ``` 10 | 5 11 | / \ 12 | 4 8 13 | / / \ 14 | 11 13 4 15 | / \ / \ 16 | 7 2 5 1 17 | ``` 18 | 19 | 返回: 20 | 21 | ``` 22 | [ 23 | [5,4,11,2], 24 | [5,8,4,5] 25 | ] 26 | ``` -------------------------------------------------------------------------------- /algorithms/96.Unique Binary Search Trees/Unique Binary Search Trees.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number} 4 | */ 5 | var numTrees = function (n) { 6 | const count = [1, 1]; 7 | for (let i = 2; i <= n; i++) { 8 | count[i] = 0; 9 | for (let j = 0; j < i; j++) { 10 | count[i] += count[j] * count[i - j - 1]; 11 | } 12 | } 13 | return count[n]; 14 | }; -------------------------------------------------------------------------------- /algorithms/461.Hamming Distance/Hamming Distance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by mr_wang on 2017/1/24. 3 | */ 4 | /** 5 | * @param {number} x 6 | * @param {number} y 7 | * @return {number} 8 | */ 9 | var hammingDistance = function(x, y) { 10 | 11 | var xor = x ^ y ; 12 | var hammingCount = 0; 13 | for(var i= 0;i<32;i++){ 14 | hammingCount += (xor>>i) & 1; 15 | } 16 | return hammingCount; 17 | }; -------------------------------------------------------------------------------- /algorithms/563.Binary Tree Tilt/README.md: -------------------------------------------------------------------------------- 1 | # Binary Tree Tilt 2 | 3 | 给定一颗二叉树,返回整个树的倾斜值。 4 | 5 | 树的节点倾斜值是指树的左子树节点之和与右子树节点之和的差的绝对值。空节点的值为0。 6 | 7 | 树的倾斜值是指所有节点倾斜之和。 8 | 9 | 例如: 10 | 11 | ``` 12 | Input: 13 | 1 14 | / \ 15 | 2 3 16 | Output: 1 17 | Explanation: 18 | Tilt of node 2 : 0 19 | Tilt of node 3 : 0 20 | Tilt of node 1 : |2-3| = 1 21 | Tilt of binary tree : 0 + 0 + 1 = 1 22 | ``` -------------------------------------------------------------------------------- /algorithms/240.Search a 2D Matrix II/README.md: -------------------------------------------------------------------------------- 1 | # Search a 2D Matrix II 2 | 3 | 编写一个有效的算法能够搜索一个 m x n 矩阵中的值,矩阵有下列特性: 4 | 5 | - 在每行从左到右的整数都是递增的 6 | - 在没列从顶到底的整数都是递增的 7 | 8 | 例如: 9 | 10 | 考虑下面的矩阵: 11 | 12 | ``` 13 | [ 14 | [1, 4, 7, 11, 15], 15 | [2, 5, 8, 12, 19], 16 | [3, 6, 9, 16, 22], 17 | [10, 13, 14, 17, 24], 18 | [18, 21, 23, 26, 30] 19 | ] 20 | ``` 21 | 22 | 给定值5,返回`true` 23 | 给定值20, 返回`false` -------------------------------------------------------------------------------- /algorithms/9. Palindrome Number/Palindrome Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} x 3 | * @return {boolean} 4 | */ 5 | var isPalindrome = function(x) { 6 | if(x < 0){ 7 | return false; 8 | } 9 | let reverse = 0, current = x; 10 | while(current > 0){ 11 | reverse = reverse * 10 + (current % 10); 12 | current = Math.floor(current / 10); 13 | } 14 | return x === reverse; 15 | }; -------------------------------------------------------------------------------- /algorithms/217.Contains Duplicate/Contains Duplicate.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {boolean} 4 | */ 5 | var containsDuplicate = function (nums) { 6 | 7 | var set = new Set(); 8 | 9 | for (let i = 0; i < nums.length; i++) { 10 | if (set.has(nums[i])) { 11 | return true 12 | } else { 13 | set.add(nums[i]); 14 | } 15 | } 16 | return false; 17 | }; -------------------------------------------------------------------------------- /algorithms/461.Hamming Distance/README.md: -------------------------------------------------------------------------------- 1 | # Hamming Distance 2 | 3 | 两个整数的汉明距离(Hamming Distance)是指两个数对应相同位置不相同位个数。 4 | 5 | 例如: 6 | 7 | Input: x = 1, y = 4 8 | 9 | Output: 2 10 | 11 | Explanation: 12 | 13 | 1 (0 0 0 1) 14 | 15 | 4 (0 1 0 0) 16 | 17 | ## JavaScript: 18 | 19 | 比较常规的思路是分别计算出出`x`和`y`的二进制数,然后再比较计算两者相同位置不相同数字的个数。 20 | 21 | 改进的思路是计算出`x`与`y`异或的数,然后分别右移,合计计算右移为1的个数。 22 | 时间复杂度为常数O(1)(原因是循环是固定次数的)。空间复杂度为O(1)。 -------------------------------------------------------------------------------- /algorithms/654.Maximum Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Maximum Binary Tree 2 | 3 | 给定一个没有重复数值的整数数组。从数组构建最大树的定义如下: 4 | 5 | 6 | - 根节点是数组中最大值。 7 | - 左子树是从最大值左侧数组构建的最大树。 8 | - 右子树是从最大值右侧数组构建的最大数。 9 | 10 | 用给定的数组构建最大树,并返回其根节点。 11 | 12 | 例1: 13 | ``` 14 | 输入: [3,2,1,6,0,5] 15 | 输出: 16 | 17 | 6 18 | / \ 19 | 3 5 20 | \ / 21 | 2 0 22 | \ 23 | 1 24 | ``` 25 | 26 | 注意: 27 | 28 | 给定数组长度的范围是: [1,1000] -------------------------------------------------------------------------------- /algorithms/508.Most Frequent Subtree Sum/README.md: -------------------------------------------------------------------------------- 1 | # Most Frequent Subtree Sum 2 | 3 | 给定一个棵树的根节点,你要找出最常出现的子树和。子树的和被定义为,以该节点为根的所有节点(包括自己)的所有节点值的和。因此,最常出现的子树和是多少,如果不止一个的话,以序列的方式返回,不计次序。 4 | 5 | 例1: 6 | 7 | 输入 8 | ``` 9 | 5 10 | / \ 11 | 2 -3 12 | ``` 13 | 14 | 返回[2, -3, 4], 因为所有的值都仅出现一次,所以以任意次序返回。 15 | 16 | 例2: 17 | 18 | 输入: 19 | 20 | ``` 21 | 5 22 | / \ 23 | 2 -5 24 | ``` 25 | 26 | 返回[2],因为2出现了两次,而-5只出现了一次。 27 | 28 | -------------------------------------------------------------------------------- /algorithms/653.Two Sum IV - Input is a BST/README.md: -------------------------------------------------------------------------------- 1 | # Two Sum IV - Input is a BST 2 | 3 | 给定一个二叉搜索树和一个目标值,如果在二叉搜索树中存在两个元素,其值和等于给定的目标值返回true,否则返回false。 4 | 5 | 例1: 6 | 7 | ``` 8 | 5 9 | / \ 10 | 3 6 11 | / \ \ 12 | 2 4 7 13 | 14 | Target = 9 15 | ``` 16 | 17 | 返回true。 18 | 19 | 例2: 20 | 21 | ``` 22 | 5 23 | / \ 24 | 3 6 25 | / \ \ 26 | 2 4 7 27 | 28 | Target = 28 29 | ``` 30 | 31 | 返回false 32 | -------------------------------------------------------------------------------- /algorithms/141.Linked List Cycle/README.md: -------------------------------------------------------------------------------- 1 | # 141. Linked List Cycle 2 | 3 | Given a linked list, determine if it has a cycle in it. 4 | 5 | Follow up: 6 | Can you solve it without using extra space? 7 | 8 | 中文翻译: 9 | 10 | 给你一个单链表,你能判断其中时候含有环。 11 | 12 | 更进一步: 13 | 14 | 你能不使用额外的空间解决该问题吗? 15 | 16 | 思路:如果不使用额外的空间来判断该单链表是否有环,可以设置两个变量分别遍历单链表,其中一个变量的遍历速度是另外一个的两倍, 17 | 这种如果不存在环,速度快的变量肯定会首先到达结尾,如果存在环,肯定会存在速度快的节点追上速度慢的节点,如果某个时刻两者相等, 18 | 肯定在单链表中存在环。 -------------------------------------------------------------------------------- /algorithms/513.Find Bottom Left Tree Value/README.md: -------------------------------------------------------------------------------- 1 | # Find Bottom Left Tree Value 2 | 3 | 给定一个二叉树,查找二叉树最后一行的最左边的元素的值。 4 | 5 | 例1: 6 | 7 | ``` 8 | Input: 9 | 10 | 2 11 | / \ 12 | 1 3 13 | 14 | Output: 15 | 1 16 | ``` 17 | 18 | 例2: 19 | 20 | ``` 21 | Input: 22 | 23 | 1 24 | / \ 25 | 2 3 26 | / / \ 27 | 4 5 6 28 | / 29 | 7 30 | 31 | Output: 32 | 7 33 | ``` 34 | 35 | 假设给定的树不为空 -------------------------------------------------------------------------------- /algorithms/606. Construct String from Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Construct String from Binary Tree 2 | 3 | 例1: 4 | 5 | ``` 6 | Input: Binary tree: [1,2,3,4] 7 | 1 8 | / \ 9 | 2 3 10 | / 11 | 4 12 | 13 | Output: "1(2(4))(3)" 14 | ``` 15 | 16 | 例2: 17 | 18 | ``` 19 | Input: Binary tree: [1,2,3,null,4] 20 | 1 21 | / \ 22 | 2 3 23 | \ 24 | 4 25 | 26 | Output: "1(2()(4))(3)" 27 | 28 | ``` -------------------------------------------------------------------------------- /algorithms/674.Longest Continuous Increasing Subsequence/README.md: -------------------------------------------------------------------------------- 1 | # Longest Continuous Increasing Subsequence 2 | 3 | 给定一个未排序的整数数组,寻找其中最长的连续增长子数组。 4 | 5 | 例1: 6 | 7 | ``` 8 | Input: [1,3,5,4,7] 9 | 10 | Output: 3 11 | 12 | Explanation: 最长子数组是 [1,3,5], 因此长度是 3. 13 | ``` 14 | 15 | 例2: 16 | 17 | ``` 18 | Input: [2,2,2,2,2] 19 | 20 | Output: 1 21 | 22 | Explanation: 最长子数组是 [2], its length is 1. 23 | ``` 24 | 25 | 注意: 26 | 27 | 数组的最大长度不超过10000 -------------------------------------------------------------------------------- /algorithms/652.Find Duplicate Subtrees/README.md: -------------------------------------------------------------------------------- 1 | # Find Duplicate Subtrees 2 | 3 | 给定一棵二叉树,返回所有重复的子树。对于每对重复的子树,你仅需要返回他们的根节点。 4 | 5 | 两棵树如果有相同的节点结构和节点值则城称其为相等的。 6 | 7 | 例1: 8 | 9 | ``` 10 | 1 11 | / \ 12 | 2 3 13 | / / \ 14 | 4 2 4 15 | / 16 | 4 17 | ``` 18 | 19 | 重复子树有: 20 | 21 | ``` 22 | 2 23 | / 24 | 4 25 | ``` 26 | 27 | 和 28 | 29 | ``` 30 | 4 31 | ``` 32 | 33 | 因此你需要返回他们根节点的列表。 -------------------------------------------------------------------------------- /algorithms/155.Min Stack/READMD.md: -------------------------------------------------------------------------------- 1 | # Min Stack 2 | 3 | 设计一个栈,支持push、pop、top 和 在常量时间中拿到最小值。 4 | 5 | push(x) -- 将元素x放入栈中 6 | pop() -- 将栈顶的元素移除 7 | top() -- 移除栈顶的元素 8 | getMin() -- 获得栈中最小的元素 9 | 10 | 例如 11 | 12 | MinStack minStack = new MinStack(); 13 | minStack.push(-2); 14 | minStack.push(0); 15 | minStack.push(-3); 16 | minStack.getMin(); --> Returns -3. 17 | minStack.pop(); 18 | minStack.top(); --> Returns 0. 19 | minStack.getMin(); --> Returns -2. -------------------------------------------------------------------------------- /algorithms/114.Flatten Binary Tree to Linked List/README.md: -------------------------------------------------------------------------------- 1 | # Flatten Binary Tree to Linked List 2 | 3 | 给定一个二叉树,将其转化为一个链表。 4 | 5 | 例如: 6 | 7 | 给定 8 | 9 | 10 | ``` 11 | 1 12 | / \ 13 | 2 5 14 | / \ \ 15 | 3 4 6 16 | ``` 17 | 18 | 将其转化为: 19 | 20 | ``` 21 | 1 22 | \ 23 | 2 24 | \ 25 | 3 26 | \ 27 | 4 28 | \ 29 | 5 30 | \ 31 | 6 32 | ``` -------------------------------------------------------------------------------- /algorithms/237.Delete Node in a Linked List/237.Delete Node in a Linked List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} node 10 | * @return {void} Do not return anything, modify node in-place instead. 11 | */ 12 | var deleteNode = function(node) { 13 | node.val = node.next.val; 14 | node.next = node.next.next; 15 | }; -------------------------------------------------------------------------------- /algorithms/2.Add Two Numbers/README.md: -------------------------------------------------------------------------------- 1 | # 2. Add Two Numbers 2 | 3 | You are given two non-empty linked lists representing two non-negative integers. 4 | The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 5 | 6 | You may assume the two numbers do not contain any leading zero, 7 | except the number 0 itself. 8 | 9 | >Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 10 | 11 | >Output: 7 -> 0 -> 8 -------------------------------------------------------------------------------- /algorithms/437.Path Sum III/README.md: -------------------------------------------------------------------------------- 1 | # Path Sum III 2 | 3 | 给定一个二叉树,其中每个节点包含一个值。 4 | 5 | 寻找路径使得节点之和等于给定的数。 6 | 7 | 这个路径不需要从根节点到叶子节点,但是必须是自顶向下的。 8 | 9 | 树的节点不会超过1000个,值的范围的在-1,000,000与1,000,000之间。 10 | 11 | 例如: 12 | 13 | ``` 14 | root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 15 | 16 | 10 17 | / \ 18 | 5 -3 19 | / \ \ 20 | 3 2 11 21 | / \ \ 22 | 3 -2 1 23 | 24 | 返回3,路径如下: 25 | 26 | 1. 5 -> 3 27 | 2. 5 -> 2 -> 1 28 | 3. -3 -> 11 29 | 30 | ``` -------------------------------------------------------------------------------- /algorithms/283.Move Zeroes/Move Zeroes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {void} Do not return anything, modify nums in-place instead. 4 | */ 5 | var moveZeroes = function (nums) { 6 | 7 | let nozeroIndex = 0; 8 | 9 | for (let i = 0; i < nums.length; i++) { 10 | if (nums[i] !== 0) { 11 | nums[nozeroIndex++] = nums[i]; 12 | } 13 | } 14 | 15 | for (let i = nozeroIndex; i < nums.length; i++) { 16 | nums[i] = 0; 17 | } 18 | 19 | }; -------------------------------------------------------------------------------- /algorithms/96.Unique Binary Search Trees/README.md: -------------------------------------------------------------------------------- 1 | # Unique Binary Search Trees 2 | 3 | 给定值n, 存在多少种结构的二叉搜索树,其中的值为1...n ? 4 | 5 | 例如: 6 | 7 | 给定值n,有下面5种的搜索二叉树。 8 | 9 | 10 | ``` 11 | 1 3 3 2 1 12 | \ / / / \ \ 13 | 3 2 1 1 3 2 14 | / / \ \ 15 | 2 1 2 3 16 | ``` 17 | 18 | Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 19 | 20 | -------------------------------------------------------------------------------- /algorithms/117.Populating Next Right Pointers in Each Node II/README.md: -------------------------------------------------------------------------------- 1 | # Populating Next Right Pointers in Each Node II 2 | 3 | 接着问题: 'Populating Next Right Pointers in Each Node' 4 | 5 | 如果给定的是任意二叉树呢?之前的方法还奏效吗? 6 | 7 | 注意: 8 | 9 | 你仅有常量的存储空间。 10 | 11 | ``` 12 | 1 13 | / \ 14 | 2 3 15 | / \ \ 16 | 4 5 7 17 | ``` 18 | 19 | 当调用完函数后,树应该像下面: 20 | 21 | ``` 22 | 1 -> NULL 23 | / \ 24 | 2 -> 3 -> NULL 25 | / \ \ 26 | 4-> 5 -> 7 -> NULL 27 | ``` -------------------------------------------------------------------------------- /algorithms/153.Find Minimum in Rotated Sorted Array/Find Minimum in Rotated Sorted Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var findMin = function (nums) { 6 | 7 | let start = 0, end = nums.length - 1; 8 | 9 | while (start < end) { 10 | let middle = Math.floor((end + start) / 2); 11 | if(nums[middle] < nums[end]){ 12 | end = middle; 13 | }else { 14 | start = middle + 1; 15 | } 16 | } 17 | return nums[start]; 18 | }; -------------------------------------------------------------------------------- /algorithms/442.Find All Duplicates in an Array/Find All Duplicates in an Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var findDuplicates = function (nums) { 6 | 7 | var result = []; 8 | 9 | nums.forEach((value, index) => { 10 | var absValue = Math.abs(value); 11 | if(nums[absValue - 1] < 0){ 12 | result.push(absValue); 13 | }else { 14 | nums[absValue - 1] = - nums[absValue -1]; 15 | } 16 | }); 17 | 18 | return result; 19 | }; -------------------------------------------------------------------------------- /algorithms/237.Delete Node in a Linked List/README.md: -------------------------------------------------------------------------------- 1 | # 237. Delete Node in a Linked List 2 | 3 | Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 4 | 5 | Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function. 6 | 7 | 8 | 中文翻译一下题目: 9 | 10 | 写一个函数来删除单链表中的一个节点(除了为节点),仅仅只给这个节点的访问。 11 | 12 | 假定这个单链表是:1 -> 2 -> 3 -> 4,并且给你值为3的第三个节点,这个单链表在你调用函数后应该变为 1 -> 2 -> 4 13 | 14 | -------------------------------------------------------------------------------- /algorithms/226.Invert Binary Tree/Invert Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {TreeNode} 11 | */ 12 | 13 | var invertTree = function (root) { 14 | if(!root){ 15 | return root; 16 | } 17 | const right = invertTree(root.left); 18 | root.left = invertTree(root.right); 19 | root.right = right; 20 | return root; 21 | }; -------------------------------------------------------------------------------- /algorithms/669.Trim a Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | # Trim a Binary Search Tree 2 | 3 | 给定一棵二叉树和最低、最高的范围边界L与R,削减这棵树使得所有的节点在范围[L, R],其中(R >= L).你可能需要改变树的根节点,使得结果返回修建后二叉树的新的根节点。 4 | 5 | 例1: 6 | ``` 7 | 输入: 8 | 1 9 | / \ 10 | 0 2 11 | 12 | L = 1 13 | R = 2 14 | 15 | 输出: 16 | 1 17 | \ 18 | 2 19 | ``` 20 | 21 | 例2: 22 | 23 | ``` 24 | 输入: 25 | 3 26 | / \ 27 | 0 4 28 | \ 29 | 2 30 | / 31 | 1 32 | 33 | L = 1 34 | R = 3 35 | 36 | 输出: 37 | 3 38 | / 39 | 2 40 | / 41 | 1 42 | ``` -------------------------------------------------------------------------------- /algorithms/169.Majority Element/Majority Element.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var majorityElement = function(nums) { 6 | 7 | let majority = null; 8 | let majorityCount = 0; 9 | 10 | nums.forEach((value)=>{ 11 | if(majorityCount === 0){ 12 | majority = value; 13 | majorityCount ++; 14 | }else if(value === majority){ 15 | majorityCount ++; 16 | }else { 17 | majorityCount --; 18 | } 19 | }); 20 | 21 | return majority; 22 | }; -------------------------------------------------------------------------------- /algorithms/26.Remove Duplicates from Sorted Array/26.Remove Duplicates from Sorted Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var removeDuplicates = function(nums) { 6 | if(!nums || nums.length <= 1){ 7 | return nums.length; 8 | } 9 | 10 | let last = nums[0]; 11 | let insertPos = 1; 12 | 13 | for(let i = 1; i < nums.length ; i++){ 14 | if(nums[i] !== last){ 15 | last = nums[i]; 16 | nums[insertPos ++] = last; 17 | } 18 | } 19 | return insertPos; 20 | }; -------------------------------------------------------------------------------- /algorithms/297.Serialize and Deserialize Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Serialize and Deserialize Binary Tree 2 | 3 | 序列化(Serialization)是一个将数据结构或者对象转化成比特序列的过程,使得其可以被存储在文件或者内存中,或者可以被网络传送,以便可以被别的计算机系统重建。 4 | 5 | 设计一个算法来系列化和重构二叉树,对系列化和重构算法应该如何工作这么不做限制。你仅需要保证二叉树可以被序列化成一个字符串并且该字符串可以被重构成之前的结构。 6 | 7 | 例如,你可以序列下面的属性结构: 8 | 9 | ``` 10 | 1 11 | / \ 12 | 2 3 13 | / \ 14 | 4 5 15 | 16 | ``` 17 | 18 | 成 "[1,2,3,null,null,4,5]",类似于LeetCode OJ 所序列化的二叉树一样。你不一定需要遵循这种结构,因此可以创造性的提出别的方法。 19 | 20 | 注意: 21 | 22 | 不要使用类、全局、静态的变量存储状态,你的序列化和重构算法应该是无状态的。 23 | 24 | -------------------------------------------------------------------------------- /algorithms/104.Maximum Depth of Binary Tree/Maximum Depth of Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var maxDepth = function (root) { 13 | 14 | return length(root); 15 | 16 | function length(node) { 17 | if (!node) { 18 | return 0; 19 | } 20 | return Math.max(length(node.left), length(node.right)) + 1; 21 | } 22 | }; -------------------------------------------------------------------------------- /algorithms/572.Subtree of Another Tree/README.md: -------------------------------------------------------------------------------- 1 | # Subtree of Another Tree 2 | 3 | 给定两个非空二叉树 s 和 t,检查树t与s的子树是否有着相同的结构与节点值。s的子树是指包含一个节点和其所有的后代节点。树可以当做自己的子树。 4 | 5 | 例如: 6 | 7 | 树s: 8 | 9 | ``` 10 | 3 11 | / \ 12 | 4 5 13 | / \ 14 | 1 2 15 | ``` 16 | 17 | 树t: 18 | 19 | ``` 20 | 4 21 | / \ 22 | 1 2 23 | ``` 24 | 25 | 返回true 26 | 27 | 树s: 28 | 29 | ``` 30 | 3 31 | / \ 32 | 4 5 33 | / \ 34 | 1 2 35 | / 36 | 0 37 | ``` 38 | 39 | 40 | 树t: 41 | 42 | ``` 43 | 4 44 | / \ 45 | 1 2 46 | ``` 47 | 48 | 返回false 49 | -------------------------------------------------------------------------------- /algorithms/538.Convert BST to Greater Tree/README.md: -------------------------------------------------------------------------------- 1 | # Convert BST to Greater Tree 2 | 3 | 给定一个二叉搜索树,将其转化成Greater Tree。Greater Tree的定义是原始BST树中的每一个节点值被改变成原始值加上在BST中所有大于其值的和。 4 | 5 | Given a Binary Search Tree (BST), 6 | convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. 7 | 8 | 9 | Example: 10 | 11 | ``` 12 | 输入: 13 | 5 14 | / \ 15 | 2 13 16 | 17 | 输出: 18 | 18 19 | / \ 20 | 20 13 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/154.Find Minimum in Rotated Sorted Array II/Find Minimum in Rotated Sorted Array II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var findMin = function(nums) { 6 | let start = 0, end = nums.length - 1; 7 | 8 | while (start < end){ 9 | let middle = Math.floor((start + end) / 2); 10 | if(nums[middle] === nums[end]){ 11 | end --; 12 | }else if(nums[middle] < nums[end]){ 13 | end = middle; 14 | }else{ 15 | start = middle + 1; 16 | 17 | } 18 | } 19 | return nums[start]; 20 | }; -------------------------------------------------------------------------------- /algorithms/83.Remove Duplicates from Sorted List/Remove Duplicates from Sorted List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} head 10 | * @return {ListNode} 11 | */ 12 | var deleteDuplicates = function(head) { 13 | var pos = head; 14 | while(pos && pos.next){ 15 | if(pos.val == pos.next.val){ 16 | pos.next = pos.next.next; 17 | }else{ 18 | pos = pos.next; 19 | } 20 | } 21 | return head; 22 | }; -------------------------------------------------------------------------------- /algorithms/674.Longest Continuous Increasing Subsequence/Longest Continuous Increasing Subsequence.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var findLengthOfLCIS = function (nums) { 6 | 7 | let curLength = nums.length <= 0 ? 0 : 1; 8 | let maxLength = curLength; 9 | 10 | for (let i = 1; i < nums.length; i++) { 11 | if (nums[i] > nums[i - 1]) { 12 | curLength++; 13 | } else { 14 | curLength = 1; 15 | } 16 | maxLength = maxLength < curLength ? curLength : maxLength; 17 | } 18 | return maxLength; 19 | }; -------------------------------------------------------------------------------- /algorithms/112.Path Sum/Path Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @param {number} sum 11 | * @return {boolean} 12 | */ 13 | var hasPathSum = function(root, sum) { 14 | 15 | if(!root){ 16 | return false; 17 | } 18 | 19 | if(!root.left && !root.right && root.val === sum){ 20 | return true; 21 | } 22 | 23 | return hasPathSum(root.left,sum - root.val) || hasPathSum(root.right,sum - root.val); 24 | 25 | }; -------------------------------------------------------------------------------- /algorithms/7.Reverse Integer/Reverse Integer.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by MrErHu on 2017/1/24. 3 | */ 4 | /** 5 | * @param {number} x 6 | * @return {number} 7 | */ 8 | var reverse = function(x) { 9 | var negtive = false; 10 | var POSTIVE_MAX = Math.pow(2,31) -1 ; 11 | var NEGTIVE_MAX = - Math.pow(2,31); 12 | var y = 0; 13 | 14 | if( x < 0 ){ 15 | negtive= true; 16 | x = -x; 17 | } 18 | 19 | while (x > 0){ 20 | y = y*10 + x%10; 21 | x = Math.floor(x/10); 22 | } 23 | 24 | y = negtive ? -y: y; 25 | 26 | return (y>POSTIVE_MAX || y { 10 | 11 | const index = Math.abs(value) - 1; 12 | 13 | if (nums[index] > 0) { 14 | nums[index] = -nums[index]; 15 | } 16 | 17 | }); 18 | 19 | nums.forEach((value, index) => { 20 | if (value > 0) { 21 | result.push(index + 1); 22 | } 23 | }); 24 | 25 | return result; 26 | }; -------------------------------------------------------------------------------- /algorithms/268.Missing Number/Missing Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var missingNumber = function (nums) { 6 | 7 | var len = nums.length; 8 | 9 | var sum = len * (len + 1) / 2; 10 | 11 | var sum1 = nums.reduce((sum, value) => value + sum, 0); 12 | 13 | return sum - sum1; 14 | 15 | }; 16 | 17 | /** 18 | * @param {number[]} nums 19 | * @return {number} 20 | */ 21 | var missingNumber = function (nums) { 22 | 23 | var num = 0; 24 | 25 | nums.forEach((value, index) => { 26 | 27 | num ^= value ^ (index + 1); 28 | }) 29 | 30 | return num; 31 | 32 | }; -------------------------------------------------------------------------------- /algorithms/671.Second Minimum Node In a Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Second Minimum Node In a Binary Tree 2 | 3 | 给定一棵非空特殊二叉树,其中的节点值都是非空的,并且树中的每个节点都有两个或者零个子节点。如果节点有两个子节点。如果节点有两个子节点,节点的值在左右小于两个自己小于两个子子节点。 4 | 5 | 给定一颗二叉树,你需要输出整个树节点值中的第二小的数。如果不存在的话,输出-1。 6 | 7 | 例1: 8 | 9 | ``` 10 | Input: 11 | 2 12 | / \ 13 | 2 5 14 | / \ 15 | 5 7 16 | 17 | Output: 5 18 | Explanation: The smallest value is 2, the second smallest value is 5. 19 | ``` 20 | 21 | 例子2: 22 | 23 | ``` 24 | Input: 25 | 2 26 | / \ 27 | 2 2 28 | 29 | Output: -1 30 | Explanation: The smallest value is 2, but there isn't any second smallest value. 31 | ``` -------------------------------------------------------------------------------- /algorithms/118.Pascal's Triangle/Pascal's Triangle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} numRows 3 | * @return {number[][]} 4 | */ 5 | var generate = function (numRows) { 6 | 7 | var result = []; 8 | 9 | if (numRows <= 0) return result; 10 | 11 | result.push([1]); 12 | 13 | if (numRows === 1) return result; 14 | 15 | for (let i = 2; i <= numRows; i++) { 16 | 17 | const level = [1]; 18 | const preLevel = result[i - 1]; 19 | 20 | for (let j = 0; j < preLevel.length - 1; j++) { 21 | level.push(preLevel[j] + preLevel[j + 1]); 22 | } 23 | 24 | level.push(1); 25 | } 26 | 27 | return result; 28 | }; -------------------------------------------------------------------------------- /algorithms/167.Two Sum II - Input array is sorted/Two Sum II - Input array is sorted.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Time Space O(n) 3 | * @param {number[]} numbers 4 | * @param {number} target 5 | * @return {number[]} 6 | */ 7 | var twoSum = function (numbers, target) { 8 | 9 | let start = 0, end = numbers.length - 1; 10 | 11 | while (start < end) { 12 | 13 | const sum = numbers[start] + numbers[end]; 14 | 15 | if (sum === target) { 16 | return [start + 1, end + 1]; 17 | } else if (sum > target) { 18 | end--; 19 | } else { 20 | start++; 21 | } 22 | } 23 | 24 | return [-1, -1]; 25 | 26 | }; -------------------------------------------------------------------------------- /algorithms/445.Add Two Numbers II/README.md: -------------------------------------------------------------------------------- 1 | # 445. Add Two Numbers II 2 | 3 | You are given two non-empty linked lists representing two non-negative integers. 4 | The most significant digit comes first and each of their nodes contain a single digit. 5 | Add the two numbers and return it as a linked list. 6 | 7 | You may assume the two numbers do not contain any leading zero, except the number 0 itself. 8 | 9 | Follow up: 10 | 11 | What if you cannot modify the input lists? In other words, reversing the lists is not allowed. 12 | 13 | Example: 14 | 15 | > 16 | Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) 17 | > 18 | Output: 7 -> 8 -> 0 -> 7 19 | 20 | 思路: 21 | 使用栈结构处理,详情见代码 -------------------------------------------------------------------------------- /algorithms/209.Minimum Size Subarray Sum/Minimum Size Subarray Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 滑动窗口法 3 | * Time: O(n) 4 | * @param {number} s 5 | * @param {number[]} nums 6 | * @return {number} 7 | */ 8 | var minSubArrayLen = function (s, nums) { 9 | 10 | let start = 0, end = 0, sum = 0; 11 | let res = nums.length + 1; 12 | 13 | while (end < nums.length) { 14 | 15 | while (sum < s && end < nums.length) { 16 | sum += nums[end++]; 17 | } 18 | 19 | while (sum >= s) { 20 | res = Math.min(res, end - start); 21 | sum -= nums[start++]; 22 | } 23 | } 24 | 25 | return res === nums.length + 1 ? 0 : res; 26 | }; -------------------------------------------------------------------------------- /algorithms/476.Number Complement/README.md: -------------------------------------------------------------------------------- 1 | # Number Complement 2 | 3 | 给定一个正整数,输出其补数。补数规则是翻转二进制表示的比特位。 4 | 5 | 提示: 6 | 7 | 1. 给定的整数保证为32位有符号整数 8 | 9 | 10 | 2. 可以假设整数的二进制表示中没有前导零位 11 | 12 | --- 13 | 14 | >Example 1: 15 | 16 | >Input: 5 17 | 18 | >Output: 2 19 | 20 | >Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. 21 | 22 | --- 23 | >Example 2: 24 | 25 | >Input: 1 26 | 27 | >Output: 0 28 | 29 | >Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. 30 | Subscribe to see which companies asked this question 31 | 32 | --- 33 | -------------------------------------------------------------------------------- /algorithms/7.Reverse Integer/README.md: -------------------------------------------------------------------------------- 1 | Reverse Integer 2 | 3 | 题目要求: 4 | 5 | 翻转整数 6 | 7 | Example1: x = 123, return 321 8 | 9 | Example2: x = -123, return -321 10 | 11 | 题目给出的提示: 12 | 13 | > 由此你想到了什么吗 14 | 15 | > 在编码前详细的提问是一个非常好的习惯,如果你在此之前仔细的思考了,对你来说是一个加分项。 16 | 17 | > 如果整数的最后一问是0,输出应该是如何的?例如:10,100 18 | 19 | > 你注意到翻转后的数组可能会出现溢出吗?假设输入是32位的整数,翻转1000000003就会出现溢出,你该何如处理 20 | 21 | > 对于该题目,我们假设如果出现了溢出则返回0 22 | 23 | 对于使用JavaScript进行解答 24 | 25 | - 我们采用数字加减的方式进行,其实对于出现100,或者10这种情况都只会返回1,所以是不存在问题的。 26 | - 其实对JavaScript的Number类型都是采用IEE745浮点数类型进行计算的,本质是不会溢出的,但是题目要求对于32未整数需要判断是否存在溢出的情况, 27 | 那么我们提前设定好32位整数所能表示的最大正整数和最小负整数,如果计算出来的数字不在该区间,我们则返回0 28 | 29 | 采用JavaScript解法空间复杂度 O(1),时间复杂度O(n) -------------------------------------------------------------------------------- /algorithms/513.Find Bottom Left Tree Value/Find Bottom Left Tree Value.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var findBottomLeftValue = function (root) { 13 | 14 | let queue = [root]; 15 | let last = null; 16 | 17 | while (queue.length > 0) { 18 | const current = queue.shift(); 19 | last = current.val; 20 | current.right && queue.push(current.right); 21 | current.left && queue.push(current.left); 22 | } 23 | 24 | return last; 25 | 26 | }; -------------------------------------------------------------------------------- /algorithms/119.Pascal's Triangle II/Pascal's Triangle II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} rowIndex 3 | * @return {number[]} 4 | */ 5 | var getRow = function (rowIndex) { 6 | 7 | var result = []; 8 | 9 | for (let i = 0; i <= rowIndex; i++) { 10 | if (i === 0 || i === rowIndex) { 11 | result.push(1); 12 | } else { 13 | result.push(compute(i, rowIndex)); 14 | } 15 | } 16 | 17 | return result; 18 | 19 | 20 | function compute(m, n) { 21 | let upper = 1, lower = 1; 22 | for (let i = n; i > m; i--) upper *= i; 23 | for (let i = (n - m); i >= 1; i--) lower *= i; 24 | return upper / lower; 25 | } 26 | }; -------------------------------------------------------------------------------- /algorithms/203.Remove Linked List Elements/Remove Linked List Elements.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} head 10 | * @param {number} val 11 | * @return {ListNode} 12 | */ 13 | var removeElements = function(head, val) { 14 | var virtualHead= new ListNode(-1); 15 | var pos = virtualHead; 16 | virtualHead.next = head; 17 | while (pos.next){ 18 | if(pos.next.val == val){ 19 | pos.next = pos.next.next; 20 | }else{ 21 | pos = pos.next; 22 | } 23 | } 24 | return virtualHead.next; 25 | }; -------------------------------------------------------------------------------- /algorithms/219.Contains Duplicate II/Contains Duplicate II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} k 4 | * @return {boolean} 5 | */ 6 | var containsNearbyDuplicate = function(nums, k) { 7 | 8 | let map = new Map(); 9 | 10 | for(let index = 0; index < nums.length ; index++){ 11 | let value = nums[index]; 12 | if(map.has(value)){ 13 | let preIndex = map.get(value); 14 | if(index - preIndex <= k){ 15 | return true 16 | }else { 17 | map.set(value, index); 18 | } 19 | }else{ 20 | map.set(value, index); 21 | } 22 | } 23 | 24 | return false; 25 | }; -------------------------------------------------------------------------------- /algorithms/617.Merge Two Binary Trees/README.md: -------------------------------------------------------------------------------- 1 | # 617. Merge Two Binary Trees 2 | 3 | 给定两个二叉树,想象用一棵树覆盖另一棵,两棵树中的部分节点可能会覆盖而另一部分不会覆盖。 4 | 5 | 你需要将它们合并成一棵新的二叉树。合并规则是如果两个节点覆盖,节点的和作为合并后的节点。否则,非空节点被作为新的二叉树的节点。 6 | 7 | 例如: 8 | 9 | Example 1: 10 | Input: 11 | Tree 1 Tree 2 12 | 1 2 13 | / \ / \ 14 | 3 2 1 3 15 | / \ \ 16 | 5 4 7 17 | Output: 18 | Merged tree: 19 | 3 20 | / \ 21 | 4 5 22 | / \ \ 23 | 5 4 7 24 | 25 | 注意: 合并的过程是从两个树的根节点开始。 26 | 27 | 思路: 28 | 29 | 我的思路是使用树的先序遍历,然后根据规则生成节点,详情见节点。 -------------------------------------------------------------------------------- /algorithms/141.Linked List Cycle/Linked List Cycle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * @param {ListNode} head 11 | * @return {boolean} 12 | */ 13 | var hasCycle = function(head) { 14 | if(!head || !head.next){ 15 | return false; 16 | } 17 | var front = head.next ? head.next.next : head.next; 18 | var end = head.next; 19 | while(front){ 20 | front = front.next ? front.next.next : front.next; 21 | end = end.next; 22 | if(front == end){ 23 | return true; 24 | } 25 | } 26 | return false; 27 | }; -------------------------------------------------------------------------------- /algorithms/238.Product of Array Except Self/Product of Array Except Self.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var productExceptSelf = function(nums) { 6 | 7 | let output = new Array(nums.length).fill(1); 8 | 9 | nums.reduce((sum,value, index)=>{ 10 | 11 | if(index + 1 < nums.length){ 12 | output[index + 1] *= sum * value; 13 | } 14 | return sum * value; 15 | 16 | },1); 17 | 18 | 19 | nums.reduceRight((sum,value, index)=>{ 20 | 21 | if(index - 1 >= 0){ 22 | output[index - 1] *= sum * value; 23 | } 24 | return sum * value; 25 | },1); 26 | 27 | return output; 28 | 29 | }; -------------------------------------------------------------------------------- /algorithms/538.Convert BST to Greater Tree/Convert BST to Greater Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {TreeNode} 11 | */ 12 | var convertBST = function (root) { 13 | 14 | 15 | convert(root, 0); 16 | 17 | return root; 18 | 19 | function convert(root, sum) { 20 | if (!root) { 21 | return sum; 22 | } 23 | 24 | const right = convert(root.right, sum); 25 | root.val += right; 26 | return root.left ? convert(root.left, root.val) : root.val; 27 | } 28 | 29 | }; -------------------------------------------------------------------------------- /algorithms/563.Binary Tree Tilt/Binary Tree Tilt.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var findTilt = function(root) { 13 | 14 | let tilt = 0; 15 | findTiltSum(root); 16 | return tilt; 17 | 18 | function findTiltSum(node) { 19 | if(!node){ 20 | return 0; 21 | } 22 | const left = findTiltSum(node.left); 23 | const right = findTiltSum(node.right); 24 | tilt += Math.abs(left - right); 25 | return left + right + node.val; 26 | } 27 | }; -------------------------------------------------------------------------------- /algorithms/108.Convert Sorted Array to Binary Search Tree/Convert Sorted Array to Binary Search Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {number[]} nums 10 | * @return {TreeNode} 11 | */ 12 | var sortedArrayToBST = function (nums) { 13 | if (!nums || nums.length <= 0) { 14 | return null; 15 | } 16 | 17 | const index = Math.floor(nums.length / 2); 18 | const node = new TreeNode(nums[index]); 19 | node.left = sortedArrayToBST(nums.slice(0, index)); 20 | node.right = sortedArrayToBST(nums.slice(index + 1)); 21 | return node; 22 | }; 23 | -------------------------------------------------------------------------------- /algorithms/450.Delete Node in a BST/README.md: -------------------------------------------------------------------------------- 1 | # Delete Node in a BST 2 | 3 | 给定一个二叉搜索树的引用和一个key值,在二叉搜索树中删除给定的节点值,并返回更新后的二叉搜索树的根节点引用。 4 | 5 | 通常,删除给定节点可以被分成两步: 6 | 7 | 寻找要删除的节点 8 | 9 | 如果发现节点,将其删除。 10 | 11 | 注意: 时间复杂度应该是O(树高) 12 | 13 | 例如: 14 | 15 | ``` 16 | root = [5,3,6,2,4,null,7] 17 | key = 3 18 | 19 | 5 20 | / \ 21 | 3 6 22 | / \ \ 23 | 2 4 7 24 | 25 | Given key to delete is 3. So we find the node with value 3 and delete it. 26 | 27 | One valid answer is [5,4,6,2,null,null,7], shown in the following BST. 28 | 29 | 5 30 | / \ 31 | 4 6 32 | / \ 33 | 2 7 34 | 35 | Another valid answer is [5,2,6,null,4,null,7]. 36 | 37 | 5 38 | / \ 39 | 2 6 40 | \ \ 41 | 4 7 42 | ``` -------------------------------------------------------------------------------- /algorithms/606. Construct String from Binary Tree/Construct String from Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} t 10 | * @return {string} 11 | */ 12 | var tree2str = function(t) { 13 | let string = '' 14 | 15 | if(!t){ 16 | return string; 17 | } 18 | 19 | string += t.val; 20 | 21 | if(!t.left && !t.right){ 22 | return string; 23 | }else if(t.left && !t.right){ 24 | return string + "(" + tree2str(t.left) + ")" 25 | }else { 26 | return string + "(" + tree2str(t.left) + ')('+tree2str(t.right)+')'; 27 | } 28 | }; -------------------------------------------------------------------------------- /algorithms/116. Populating Next Right Pointers in Each Node/README.md: -------------------------------------------------------------------------------- 1 | # Populating Next Right Pointers in Each Node 2 | 3 | 给定一颗二叉树: 4 | 5 | ``` 6 | struct TreeLinkNode { 7 | TreeLinkNode *left; 8 | TreeLinkNode *right; 9 | TreeLinkNode *next; 10 | } 11 | ``` 12 | 13 | 将每一个节点的next连接到右边的节点。如果不存在右节点,next指针应该为null、 14 | 15 | 初始时,所有的next节点都是null。 16 | 17 | 注意: 18 | 19 | - 你仅可以使用常量的存储空间。 20 | 21 | - 你可以假设是一颗完美二叉树(所有的叶子节点都在同一层,每一个父节点都有两个字节点) 22 | 23 | 例如: 24 | 25 | ``` 26 | 1 27 | / \ 28 | 2 3 29 | / \ / \ 30 | 4 5 6 7 31 | ``` 32 | 33 | 调用函数后你的节点应该是: 34 | 35 | ``` 36 | 1 -> NULL 37 | / \ 38 | 2 -> 3 -> NULL 39 | / \ / \ 40 | 4->5->6->7 -> NULL 41 | ``` -------------------------------------------------------------------------------- /algorithms/643.Maximum Average Subarray I/Maximum Average Subarray I.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} k 4 | * @return {number} 5 | */ 6 | var findMaxAverage = function (nums, k) { 7 | if (nums.length < k) { 8 | throw new Error('Input Error'); 9 | return -1; 10 | } 11 | let startIndex = 0, endIndex = k - 1; 12 | let sum = 0; 13 | for (let i = 0; i < k; i++) { 14 | sum += nums[i]; 15 | } 16 | let maxSum = sum; 17 | for (endIndex++; endIndex < nums.length; endIndex++) { 18 | sum += nums[endIndex]; 19 | sum -= nums[startIndex++]; 20 | 21 | if (sum > maxSum) { 22 | maxSum = sum; 23 | } 24 | } 25 | return maxSum / k; 26 | 27 | }; -------------------------------------------------------------------------------- /algorithms/100.Same Tree/Same tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} p 10 | * @param {TreeNode} q 11 | * @return {boolean} 12 | */ 13 | var isSameTree = function (p, q) { 14 | 15 | return checkSame(p, q); 16 | 17 | function checkSame(pNode, qNode) { 18 | if (!pNode && !qNode) { 19 | return true; 20 | } 21 | 22 | if ((pNode && !qNode) || (!pNode && qNode) || (pNode.val != qNode.val)) { 23 | return false; 24 | } 25 | 26 | return checkSame(pNode.left, qNode.left) && checkSame(pNode.right, qNode.right); 27 | } 28 | 29 | }; -------------------------------------------------------------------------------- /algorithms/669.Trim a Binary Search Tree/Trim a Binary Search Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @param {number} L 11 | * @param {number} R 12 | * @return {TreeNode} 13 | */ 14 | var trimBST = function (root, L, R) { 15 | if (!root) { 16 | return null; 17 | } 18 | 19 | if(root.val < L){ 20 | return trimBST(root.right,L,R); 21 | } 22 | 23 | if(root.val > R){ 24 | return trimBST(root.left,L,R); 25 | } 26 | 27 | root.left = trimBST(root.left, L, R); 28 | root.right = trimBST(root.right, L, R); 29 | 30 | return root; 31 | }; -------------------------------------------------------------------------------- /algorithms/35.Search Insert Position/Search Insert Position.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number} 5 | */ 6 | var searchInsert = function (nums, target) { 7 | 8 | let start = 0, end = nums.length - 1; 9 | 10 | while (start <= end) { 11 | let mid = Math.floor((start + end) / 2); 12 | if (nums[mid] === target) { 13 | return mid; 14 | } 15 | 16 | if (start === end) { 17 | return target > nums[start] ? start + 1 : start; 18 | } 19 | 20 | if (nums[mid] > target) { 21 | end = mid - 1; 22 | } else { 23 | start = mid + 1; 24 | } 25 | } 26 | 27 | return target > nums[start] ? start + 1 : start; 28 | }; -------------------------------------------------------------------------------- /algorithms/61.Rotate List/Rotate List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} head 10 | * @param {number} k 11 | * @return {ListNode} 12 | */ 13 | var rotateRight = function(head, k) { 14 | if(!head || !head.next){ 15 | return head; 16 | } 17 | var length = 1; 18 | var pos = head; 19 | while(pos.next){ 20 | length++ 21 | pos = pos.next; 22 | } 23 | pos.next = head; 24 | length = length - (k % length); 25 | 26 | while(length > 0){ 27 | length--; 28 | pos = pos.next; 29 | } 30 | var tempHead = pos.next; 31 | pos.next = null; 32 | return tempHead; 33 | }; -------------------------------------------------------------------------------- /algorithms/129.Sum Root to Leaf Numbers/Sum Root to Leaf Numbers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var sumNumbers = function(root) { 13 | let sum = 0; 14 | traversal(root,[]); 15 | return sum; 16 | 17 | function traversal(root, stack) { 18 | if(!root){ 19 | return; 20 | } 21 | stack.push(root.val); 22 | if(!root.left && !root.right){ 23 | sum += stack.reduce((sum ,cur)=> sum * 10 + cur,0); 24 | } 25 | traversal(root.left, stack); 26 | traversal(root.right, stack); 27 | stack.pop(); 28 | } 29 | }; -------------------------------------------------------------------------------- /algorithms/111.Minimum Depth of Binary Tree/Minimum Depth of Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var minDepth = function (root) { 13 | 14 | return length(root); 15 | 16 | function length(node) { 17 | if (!node) { 18 | return 0; 19 | } 20 | 21 | if (node.left && !node.right) { 22 | return length(node.left) + 1 23 | } 24 | 25 | if (node.right && !node.left) { 26 | return length(node.right) + 1 27 | } 28 | 29 | return Math.min(length(node.left), length(node.right)) + 1; 30 | } 31 | 32 | }; -------------------------------------------------------------------------------- /algorithms/98.Validate Binary Search Tree/Validate Binary Search Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * inorder 10 | * @param {TreeNode} root 11 | * @return {boolean} 12 | */ 13 | var isValidBST = function (root) { 14 | 15 | let preNodeVal = -Infinity; 16 | return checkValid(root); 17 | 18 | function checkValid(node) { 19 | if(!node){ 20 | return true; 21 | } 22 | if(!checkValid(node.left)){ 23 | return false; 24 | } 25 | if(preNodeVal >= node.val){ 26 | return false; 27 | } 28 | preNodeVal = node.val; 29 | return checkValid(node.right); 30 | } 31 | }; -------------------------------------------------------------------------------- /algorithms/235.Lowest Common Ancestor of a Binary Search Tree/Lowest Common Ancestor of a Binary Search Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @param {TreeNode} p 11 | * @param {TreeNode} q 12 | * @return {TreeNode} 13 | */ 14 | var lowestCommonAncestor = function(root, p, q) { 15 | 16 | let current = root; 17 | 18 | while (current){ 19 | if(current.val < p.val && current.val < q.val){ 20 | current = current.right; 21 | }else if(current.val > p.val && current.val > q.val){ 22 | current = current.left 23 | }else{ 24 | break; 25 | } 26 | } 27 | 28 | return current; 29 | }; -------------------------------------------------------------------------------- /algorithms/230.Kth Smallest Element in a BST/Kth Smallest Element in a BST.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * Recursive Version 10 | * @param {TreeNode} root 11 | * @param {number} k 12 | * @return {number} 13 | */ 14 | var kthSmallest = function (root, k) { 15 | 16 | let kth = k; 17 | return traversal(root); 18 | 19 | function traversal(node) { 20 | if (!node) { 21 | return; 22 | } 23 | const left = traversal(node.left); 24 | if (kth <= 0) { 25 | return left; 26 | } 27 | 28 | if (--kth <= 0) { 29 | return node.val; 30 | } 31 | 32 | return traversal(node.right); 33 | } 34 | }; -------------------------------------------------------------------------------- /algorithms/236.Lowest Common Ancestor of a Binary Tree/Lowest Common Ancestor of a Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * Recursion 10 | * @param {TreeNode} root 11 | * @param {TreeNode} p 12 | * @param {TreeNode} q 13 | * @return {TreeNode} 14 | */ 15 | var lowestCommonAncestor = function (root, p, q) { 16 | 17 | if (!root || root == p || root == q) { 18 | return root; 19 | } 20 | 21 | const left = lowestCommonAncestor(root.left, p, q); 22 | const right = lowestCommonAncestor(root.right, p, q); 23 | 24 | if(left && right){ 25 | return root; 26 | }else if(left){ 27 | return left 28 | }else { 29 | return right; 30 | } 31 | 32 | }; -------------------------------------------------------------------------------- /algorithms/19.Remove Nth Node From End of List/Remove Nth Node From End of List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} head 10 | * @param {number} n 11 | * @return {ListNode} 12 | */ 13 | 14 | var removeNthFromEnd = function(head, n) { 15 | 16 | if(n <= 0 || head == null){ 17 | return head; 18 | } 19 | //create virtual-head 20 | const virtualHead = new ListNode(-1); 21 | virtualHead.next = head; 22 | let front = virtualHead, end = virtualHead; 23 | while (front.next){ 24 | front = front.next; 25 | if(n <= 0){ 26 | end = end.next; 27 | } 28 | n --; 29 | } 30 | end.next = end.next.next; 31 | return virtualHead.next; 32 | }; -------------------------------------------------------------------------------- /algorithms/110.Balanced Binary Tree/Balanced Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {boolean} 11 | */ 12 | var isBalanced = function (root) { 13 | return checkBalanced(root) != -1; 14 | /** 15 | * @param node 16 | * @returns {number} 17 | */ 18 | function checkBalanced(node) { 19 | 20 | if (!node) { 21 | return 0; 22 | } 23 | 24 | const left = checkBalanced(node.left); 25 | const right = checkBalanced(node.right); 26 | 27 | if (left === -1 || right === -1 || Math.abs(left - right) > 1) { 28 | return -1; 29 | } 30 | 31 | return Math.max(left, right) + 1; 32 | } 33 | }; -------------------------------------------------------------------------------- /algorithms/222.Count Complete Tree Nodes/Count Complete Tree Nodes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var countNodes = function(root) { 13 | 14 | if(root === null){ 15 | return 0; 16 | } 17 | 18 | let leftmost = 0, rightmost = 0; 19 | let left = root.left, right = root.right; 20 | while (left){ 21 | left = left.left; 22 | leftmost ++; 23 | } 24 | while (right){ 25 | right = right.right; 26 | rightmost ++; 27 | } 28 | if(leftmost === rightmost){ 29 | return Math.pow(2, leftmost + 1) - 1; 30 | } 31 | 32 | return countNodes(root.left) + countNodes(root.right) + 1; 33 | 34 | }; -------------------------------------------------------------------------------- /algorithms/543.Diameter of Binary Tree/Diameter of Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var diameterOfBinaryTree = function (root) { 13 | 14 | if (!root) { 15 | return 0; 16 | } 17 | 18 | let maxDiameter = -Infinity; 19 | depth(root); 20 | return maxDiameter; 21 | 22 | function depth(root) { 23 | if (!root) { 24 | return 0; 25 | } 26 | const left = depth(root.left); 27 | const right = depth(root.right); 28 | if(left + right > maxDiameter){ 29 | maxDiameter = left + right; 30 | } 31 | return Math.max(left, right) + 1; 32 | } 33 | 34 | }; -------------------------------------------------------------------------------- /algorithms/114.Flatten Binary Tree to Linked List/Flatten Binary Tree to Linked List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {void} Do not return anything, modify root in-place instead. 11 | */ 12 | var flatten = function(root) { 13 | 14 | let preNode = null; 15 | traversal(root); 16 | 17 | 18 | function traversal(node) { 19 | 20 | if(!node){ 21 | return; 22 | } 23 | 24 | if(preNode != null){ 25 | preNode.right = node; 26 | preNode.left = null; 27 | } 28 | 29 | preNode = node; 30 | const right = node.right; 31 | traversal(node.left); 32 | traversal(right); 33 | } 34 | 35 | }; -------------------------------------------------------------------------------- /algorithms/142.Linked List Cycle II/Linked List Cycle II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * @param {ListNode} head 11 | * @return {ListNode} 12 | */ 13 | var detectCycle = function (head) { 14 | 15 | var oneStep = head; 16 | var twoStep = head; 17 | 18 | while (twoStep != null) { 19 | 20 | oneStep = oneStep.next; 21 | 22 | twoStep = twoStep.next; 23 | twoStep = twoStep ? twoStep.next : twoStep; 24 | 25 | if(oneStep == twoStep){ 26 | break; 27 | } 28 | } 29 | 30 | if(!twoStep){ 31 | return null; 32 | } 33 | 34 | while (head!= oneStep){ 35 | head = head.next; 36 | oneStep = oneStep.next; 37 | } 38 | 39 | return oneStep; 40 | }; -------------------------------------------------------------------------------- /algorithms/113.Path Sum II/Path Sum II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @param {number} sum 11 | * @return {number[][]} 12 | */ 13 | var pathSum = function(root, sum) { 14 | 15 | const result = [], path = []; 16 | findPath(root,sum); 17 | return result; 18 | 19 | function findPath(root,sum) { 20 | if(!root){ 21 | return; 22 | } 23 | 24 | path.push(root.val); 25 | 26 | if(!root.left && !root.right && root.val === sum){ 27 | result.push(path.slice(0)); 28 | }else{ 29 | findPath(root.left,sum - root.val); 30 | findPath(root.right,sum - root.val); 31 | } 32 | path.pop(); 33 | } 34 | 35 | }; -------------------------------------------------------------------------------- /algorithms/199.Binary Tree Right Side View/Binary Tree Right Side View.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[]} 11 | */ 12 | var rightSideView = function (root) { 13 | 14 | if (!root) { 15 | return []; 16 | } 17 | 18 | const queue = [root], result = []; 19 | let levelEnd = root; 20 | 21 | while (queue.length > 0 ){ 22 | const currentNode = queue.shift(); 23 | currentNode.left && queue.push(currentNode.left); 24 | currentNode.right && queue.push(currentNode.right); 25 | if(levelEnd === currentNode){ 26 | result.push(levelEnd.val); 27 | levelEnd = queue[queue.length -1]; 28 | } 29 | } 30 | return result; 31 | }; -------------------------------------------------------------------------------- /algorithms/572.Subtree of Another Tree/Subtree of Another Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} s 10 | * @param {TreeNode} t 11 | * @return {boolean} 12 | */ 13 | var isSubtree = function (s, t) { 14 | 15 | if(!s && !t){ 16 | return true; 17 | } 18 | 19 | if(!s && t){ 20 | return false; 21 | } 22 | 23 | return checkSame(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t); 24 | 25 | function checkSame(s, t) { 26 | if (!s && !t) { 27 | return true; 28 | } 29 | 30 | if (s == null || t == null || s.val != t.val) { 31 | return false; 32 | } 33 | 34 | return checkSame(s.left, t.left) && checkSame(s.right, t.right); 35 | } 36 | 37 | }; -------------------------------------------------------------------------------- /algorithms/21.Merge Two Sorted Lists/Merge Two Sorted Lists.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} l1 10 | * @param {ListNode} l2 11 | * @return {ListNode} 12 | */ 13 | var mergeTwoLists = function (l1, l2) { 14 | var virtualHead = new ListNode(-1); 15 | var pos = virtualHead; 16 | while (l1 && l2) { 17 | let temp; 18 | if (l1.val > l2.val) { 19 | temp = l2; 20 | l2 = l2.next; 21 | } else { 22 | temp = l1; 23 | l1 = l1.next; 24 | } 25 | temp.next = null; 26 | pos.next = temp; 27 | pos = pos.next; 28 | } 29 | if (l1) { 30 | pos.next = l1; 31 | } 32 | if (l2) { 33 | pos.next = l2; 34 | } 35 | return virtualHead.next; 36 | }; -------------------------------------------------------------------------------- /algorithms/617.Merge Two Binary Trees/Merge Two Binary Trees.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} t1 10 | * @param {TreeNode} t2 11 | * @return {TreeNode} 12 | */ 13 | var mergeTrees = function mergeTrees(t1, t2) { 14 | 15 | let node = null; 16 | 17 | if (t1 != null && t2 != null) { 18 | node = new TreeNode(t1.val + t2.val); 19 | } else if (t1 == null && t2 != null) { 20 | node = new TreeNode(t2.val) 21 | } else if (t2 == null && t1 != null) { 22 | node = new TreeNode(t1.val) 23 | } 24 | 25 | 26 | if (node != null) { 27 | node.left = mergeTrees(t1 ? t1.left : null, t2 ? t2.left : null); 28 | node.right = mergeTrees(t1 ? t1.right : null, t2 ? t2.right : null); 29 | } 30 | return node; 31 | }; -------------------------------------------------------------------------------- /algorithms/33.Search in Rotated Sorted Array/Search in Rotated Sorted Array.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number} 5 | */ 6 | var search = function(nums, target) { 7 | 8 | let low = 0, high = nums.length - 1; 9 | 10 | while (low <= high){ 11 | 12 | let mid = Math.floor((low + high) / 2); 13 | 14 | if(nums[mid] === target){ 15 | return mid; 16 | } 17 | 18 | if(nums[mid] <= nums[high]){ 19 | if(target >= nums[mid] && target <= nums[high]){ 20 | low = mid + 1; 21 | }else { 22 | high = mid - 1; 23 | } 24 | }else { 25 | if(target >= nums[low] && target <= nums[mid]){ 26 | high = mid - 1; 27 | }else { 28 | low = mid + 1; 29 | } 30 | 31 | } 32 | } 33 | return -1; 34 | 35 | }; -------------------------------------------------------------------------------- /algorithms/102.Binary Tree Level Order Traversal/Binary Tree Level Order Traversal.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[][]} 11 | */ 12 | var levelOrder = function (root) { 13 | 14 | if(!root){ 15 | return [] 16 | } 17 | 18 | let queue = [root], result = [], level = []; 19 | let levelEnd = root; 20 | 21 | while (queue.length > 0) { 22 | let current = queue.shift(); 23 | level.push(current.val); 24 | current.left && queue.push(current.left); 25 | current.right && queue.push(current.right); 26 | if(levelEnd === current){ 27 | result.push(level); 28 | level = []; 29 | levelEnd = queue[queue.length - 1]; 30 | } 31 | } 32 | 33 | return result; 34 | 35 | }; -------------------------------------------------------------------------------- /algorithms/4.Median of Two Sorted Arrays/README.md: -------------------------------------------------------------------------------- 1 | # 4. Median of Two Sorted Arrays 2 | 3 | 有两个已经排序的数组nums1和nums2,其长度分别是m和n。 4 | 5 | 找出两个数组中间的树。总的运行时间不能超过O(log(m+n)) 6 | 7 | Example 1: 8 | 9 | >nums1 = [1, 3] 10 | >nums2 = [2] 11 | > 12 | >The median is 2.0 13 | 14 | Example 2: 15 | 16 | >nums1 = [1, 3] 17 | >nums2 = [2] 18 | > 19 | >The median is 2.0 20 | 21 | ## 思路: 22 | 23 |    本题难度比较大,思路是从网上找到的: 24 | 25 | 需要用到一个推导公式,如果存在两个数组: A B ,其长度分别是m n 26 | 27 | 那么数组A中项目为 A1......Am,数组B中的项目为 B1......Bn。假设存在一个i,j使得 Ai < Bj,必能保证 A1...Ai都小于(A + B)i+j(+表示并集) 28 | 29 | 上面的思路可以用来查找两个数组中第n个小的数 30 | 31 | 假设我们要查找两个数组中第K个小的数字,我们首先找到A数组中A(k/2),然后找到B中的B(K/2), 假设 A(k/2) < B(K/2),那么可以保证 A1......A(k/2) 都是小于(A+B)K 32 | 33 | 因此就可以排除 A1......A(k/2),所以我们一次查找的是第(k - k/2)小的数,直到 k = 1 时,我们可以选取两个数组中数组的头元素中最小的一个。 34 | 35 | 我之前纠结的纠结的一个点是,如果K是奇数怎么办,其实仔细想想如果K是奇数也没有什么,k / 2 === ( (k-1) / 2 ),这时相比于偶数也只不过是少剔除一个元素, 36 | 37 | 而且A1......A((k-1) / 2 )肯定小于(A + B)k 38 | 39 | 有个这个函数解答就会非常的简单了,可以看看代码 -------------------------------------------------------------------------------- /algorithms/437.Path Sum III/Path Sum III.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @param {number} sum 11 | * @return {number} 12 | */ 13 | var pathSum = function (root, sum) { 14 | 15 | const path = []; 16 | return findPath(root, sum); 17 | 18 | 19 | function findPath(root, sum) { 20 | if (!root) { 21 | return 0; 22 | } 23 | 24 | path.push(root.val); 25 | 26 | let pathSum = 0, count = 0; 27 | for (let i = path.length - 1; i >= 0; i--) { 28 | pathSum += path[i]; 29 | if (pathSum == sum) { 30 | count++; 31 | } 32 | } 33 | 34 | const rs = count + findPath(root.left, sum) + findPath(root.right, sum); 35 | path.pop(); 36 | return rs; 37 | } 38 | }; -------------------------------------------------------------------------------- /algorithms/671.Second Minimum Node In a Binary Tree/Second Minimum Node In a Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var findSecondMinimumValue = function (root) { 13 | 14 | if (!root || (!root.left && !root.right)) { 15 | return -1; 16 | } 17 | 18 | let left = root.left.val; 19 | let right = root.right.val; 20 | 21 | if (left === root.val) { 22 | left = findSecondMinimumValue(root.left); 23 | } 24 | 25 | if (right === root.val) { 26 | right = findSecondMinimumValue(root.right); 27 | } 28 | 29 | if (left !== -1 && right !== -1) { 30 | return Math.min(left, right); 31 | } else if (left !== -1) { 32 | return left; 33 | } else { 34 | return right; 35 | } 36 | 37 | }; -------------------------------------------------------------------------------- /algorithms/653.Two Sum IV - Input is a BST/Two Sum IV - Input is a BST.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * 空间复杂度为n 时间复杂度n 10 | * @param {TreeNode} root 11 | * @param {number} k 12 | * @return {boolean} 13 | */ 14 | var findTarget = function (root, k) { 15 | const array = []; 16 | (function traversal(node) { 17 | if (!node) { 18 | return; 19 | } 20 | traversal(node.left); 21 | if (node) { 22 | array.push(node.val) 23 | } 24 | traversal(node.right); 25 | })(root); 26 | let i = 0, j = array.length - 1; 27 | while (i < j) { 28 | if (array[i] + array[j] === k) { 29 | return true; 30 | } else if (array[i] + array[j] > k) { 31 | j--; 32 | } else { 33 | i++ 34 | } 35 | } 36 | return false; 37 | }; -------------------------------------------------------------------------------- /algorithms/107.Binary Tree Level Order Traversal II/Binary Tree Level Order Traversal II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * 利用JavaScript中数组的reverse函数 10 | * @param {TreeNode} root 11 | * @return {number[][]} 12 | */ 13 | var levelOrderBottom = function(root) { 14 | 15 | if(!root){ 16 | return [] 17 | } 18 | 19 | let queue = [root], result = [], level = []; 20 | let levelEnd = root; 21 | 22 | while (queue.length > 0) { 23 | let current = queue.shift(); 24 | level.push(current.val); 25 | current.left && queue.push(current.left); 26 | current.right && queue.push(current.right); 27 | if(levelEnd === current){ 28 | result.unshift(level); 29 | level = []; 30 | levelEnd = queue[queue.length - 1]; 31 | } 32 | } 33 | 34 | return result; 35 | 36 | }; -------------------------------------------------------------------------------- /algorithms/623.Add One Row to Tree/README.md: -------------------------------------------------------------------------------- 1 | # Add One Row to Tree 2 | 3 | 给定一棵二叉树,和值v和深入d。你需要添加一行节点,其中节点的值为n,给定的深度为d。 4 | 5 | 给定下面的规则: 6 | 7 | 给定一个非负的深度d,在树的d-1层的所有非空节点,创建两个值为n的节点分别作为左右子树。 8 | 9 | 并且N的原始左子树应该作为新创建的左子树的左子树,原始的右子树应该作为新创建的右子树的右子树。 10 | 11 | 如果d的深度为1意味着不存在d-1层,创建一个树节点,其值为v,作为原始树的根节点,原始的树作为新的左子树。 12 | 13 | 例1: 14 | 15 | ``` 16 | Input: 17 | A binary tree as following: 18 | 4 19 | / \ 20 | 2 6 21 | / \ / 22 | 3 1 5 23 | 24 | v = 1 25 | 26 | d = 2 27 | 28 | Output: 29 | 4 30 | / \ 31 | 1 1 32 | / \ 33 | 2 6 34 | / \ / 35 | 3 1 5 36 | ``` 37 | 38 | 例2: 39 | 40 | ``` 41 | Input: 42 | A binary tree as following: 43 | 4 44 | / 45 | 2 46 | / \ 47 | 3 1 48 | 49 | v = 1 50 | 51 | d = 3 52 | 53 | Output: 54 | 4 55 | / 56 | 2 57 | / \ 58 | 1 1 59 | / \ 60 | 3 1 61 | 62 | ``` 63 | 64 | 注意: 65 | 66 | - 给定d值的范围是[1, 树的最大深度 + 1] 67 | - 给定的树至少有一个根节点 68 | 69 | -------------------------------------------------------------------------------- /algorithms/92.Reverse Linked List II/Reverse Linked List II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} head 10 | * @param {number} m 11 | * @param {number} n 12 | * @return {ListNode} 13 | */ 14 | var reverseBetween = function (head, m, n) { 15 | let virtualHead = new ListNode(-1); 16 | virtualHead.next = head; 17 | 18 | let point = head, prePoint = virtualHead, index = 1; 19 | 20 | while (index < m) { 21 | prePoint = point; 22 | point = point.next; 23 | index++; 24 | } 25 | 26 | let next = point.next; 27 | let temp = next; 28 | let tail = point; 29 | 30 | while (index < n) { 31 | temp = next.next; 32 | next.next = point; 33 | point = next; 34 | next = temp; 35 | index++; 36 | } 37 | prePoint.next = point; 38 | tail.next = temp; 39 | return virtualHead.next; 40 | }; -------------------------------------------------------------------------------- /algorithms/328.Odd Even Linked List/Odd Even Linked List.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} head 10 | * @return {ListNode} 11 | */ 12 | 13 | var oddEvenList = function(head) { 14 | 15 | var oddHead = new ListNode(-1); 16 | var evenHead = new ListNode(-1); 17 | var oddPoint = oddHead, evenPoint = evenHead; 18 | 19 | if(!head){ 20 | return head; 21 | } 22 | 23 | var cursor = head, cursorCount = 1; 24 | 25 | while(cursor){ 26 | if(cursorCount % 2 === 1){ 27 | oddPoint.next = cursor; 28 | oddPoint = oddPoint.next; 29 | }else{ 30 | evenPoint.next = cursor; 31 | evenPoint = evenPoint.next; 32 | } 33 | cursor = cursor.next; 34 | cursorCount ++; 35 | } 36 | 37 | evenPoint.next = null; 38 | oddPoint.next = evenHead.next; 39 | return oddHead.next; 40 | }; -------------------------------------------------------------------------------- /algorithms/637.Average of Levels in Binary Tree/Average of Levels in Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[]} 11 | */ 12 | var averageOfLevels = function(root) { 13 | if(!root){ 14 | return [] 15 | } 16 | 17 | const result = [], queue = [root]; 18 | let levelEnd = root, levelSum = 0, levelCount = 0; 19 | 20 | 21 | while(queue.length > 0){ 22 | const current = queue.shift(); 23 | levelSum += current.val; 24 | levelCount ++; 25 | current.left && queue.push(current.left); 26 | current.right && queue.push(current.right); 27 | if(current == levelEnd){ 28 | result.push(levelSum / levelCount); 29 | levelEnd = queue[queue.length - 1]; 30 | levelCount = levelSum = 0; 31 | } 32 | } 33 | 34 | return result; 35 | 36 | }; -------------------------------------------------------------------------------- /algorithms/515.Find Largest Value in Each Tree Row/Find Largest Value in Each Tree Row.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[]} 11 | */ 12 | var largestValues = function(root) { 13 | const rs = []; 14 | if(!root){ 15 | return rs; 16 | } 17 | 18 | const queue = [root]; 19 | let levelEnd = root, levelMax = root.val; 20 | 21 | while (queue.length > 0 ){ 22 | const current = queue.shift(); 23 | if(levelMax < current.val){ 24 | levelMax = current.val; 25 | } 26 | current.left && queue.push(current.left); 27 | current.right && queue.push(current.right); 28 | 29 | if(levelEnd === current){ 30 | levelEnd = queue[queue.length - 1]; 31 | rs.push(levelMax); 32 | levelMax = -Infinity; 33 | } 34 | } 35 | return rs; 36 | }; -------------------------------------------------------------------------------- /algorithms/78.Subsets/Subsets.js: -------------------------------------------------------------------------------- 1 | /** 2 | * non-recursion 3 | * @param {number[]} nums 4 | * @return {number[][]} 5 | */ 6 | var subsets = function (nums) { 7 | 8 | const result = [[]]; 9 | 10 | for (let i = 0; i < nums.length; i++) { 11 | 12 | for (let j = 0; j < result.length; j++) { 13 | 14 | result.push(result[j].concat().push(nums[i])); 15 | } 16 | 17 | } 18 | 19 | return result; 20 | }; 21 | 22 | 23 | var subsets = function (nums) { 24 | 25 | const result = []; 26 | const len = Math.pow(2, nums.length); 27 | 28 | for (let cur = 0; cur < len; cur++) { 29 | let binary = cur.toString(2); 30 | while (binary.length < nums.length){ 31 | binary = '0' + binary; 32 | } 33 | const level = []; 34 | for(let i = 0; i < nums.length; i++){ 35 | if(binary[i] === '1'){ 36 | level.push(nums[i]); 37 | } 38 | } 39 | result.push(level); 40 | } 41 | 42 | return result; 43 | 44 | }; -------------------------------------------------------------------------------- /algorithms/654.Maximum Binary Tree/Maximum Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {number[]} nums 10 | * @return {TreeNode} 11 | */ 12 | var constructMaximumBinaryTree = function (nums) { 13 | 14 | return construct(nums, 0, nums.length - 1); 15 | 16 | function construct(array, startIndex, endIndex) { 17 | if (startIndex > endIndex) { 18 | return null; 19 | } 20 | let maxValue = -Infinity, maxIndex = -1; 21 | for (let i = startIndex; i <= endIndex; i++) { 22 | if (maxValue < array[i]) { 23 | maxValue = array[i]; 24 | maxIndex = i; 25 | 26 | } 27 | } 28 | const node = new TreeNode(maxValue); 29 | node.left = construct(array, startIndex, maxIndex - 1); 30 | node.right = construct(array, maxIndex + 1, endIndex); 31 | return node; 32 | } 33 | 34 | }; -------------------------------------------------------------------------------- /algorithms/103.Binary Tree Zigzag Level Order Traversal/Binary Tree Zigzag Level Order Traversal.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[][]} 11 | */ 12 | var zigzagLevelOrder = function (root) { 13 | if (!root) { 14 | return []; 15 | } 16 | 17 | let level = [], result = [], queue = [root]; 18 | let levelEnd = root, levelNum = 0; 19 | 20 | while (queue.length > 0) { 21 | const current = queue.shift(); 22 | levelNum % 2 == 0 ? (level.push(current.val)) : (level.unshift(current.val)); 23 | current.left && queue.push(current.left); 24 | current.right && queue.push(current.right); 25 | if (levelEnd == current) { 26 | levelNum++; 27 | levelEnd = queue[queue.length - 1]; 28 | result.push(level); 29 | level = []; 30 | } 31 | } 32 | 33 | return result; 34 | 35 | }; -------------------------------------------------------------------------------- /algorithms/160.Intersection of Two Linked Lists/README.md: -------------------------------------------------------------------------------- 1 | # 160. Intersection of Two Linked Lists 2 | 3 | Write a program to find the node at which the intersection of two singly linked lists begins. 4 | 5 | For example, the following two linked lists: 6 | 7 | A: a1 → a2 8 | 9 | ↘ 10 | 11 | c1 → c2 → c3 12 | 13 | ↗ 14 | 15 | B: b1 → b2 → b3 16 | 17 | begin to intersect at node c1. 18 | 19 | Notes: 20 | 21 | 1. If the two linked lists have no intersection at all, return null. 22 | 2. The linked lists must retain their original structure after the function returns. 23 | 3. You may assume there are no cycles anywhere in the entire linked structure. 24 | 4. Your code should preferably run in O(n) time and use only O(1) memory. 25 | 26 | 27 | 思路解析: 28 | 29 | 如果要使用时间复杂度为O(n),空间复杂度为O(1),方法为: 30 | 31 | 1. 首先分别遍历单链表A,得到长度Alength 32 | 33 | 2. 再次遍历单链表B,得到长度Blength 34 | 35 | 3. 选取长度较长的节点,先遍历两个长度之差(因为如果存在交叉点,肯定会存在的情况是后面的节点是完全相同的),得到节点C 36 | 37 | 4. 依次同速度遍历节点C为首的单链表和较短的单链表,如果存在两个节点相同,则返回,否则返回null 38 | 39 | -------------------------------------------------------------------------------- /algorithms/349.Intersection of Two Arrays/Intersection of Two Arrays.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 利用ES6 3 | * @param {number[]} nums1 4 | * @param {number[]} nums2 5 | * @return {number[]} 6 | */ 7 | var intersection = function(nums1, nums2) { 8 | const set1 = new Set(nums1); 9 | const set2 = new Set(nums2); 10 | const result = []; 11 | set2.forEach((val)=>{ 12 | set1.has(val) && (result.push(val)); 13 | }); 14 | return result; 15 | }; 16 | 17 | 18 | 19 | /** 20 | * @param {number[]} nums1 21 | * @param {number[]} nums2 22 | * @return {number[]} 23 | */ 24 | var intersection = function(nums1, nums2) { 25 | 26 | var dict_1 = {}; 27 | 28 | for(var i=0;i= 0 && j < n) { 16 | 17 | if (matrix[i][j] === target) { 18 | return true; 19 | } 20 | 21 | if (i == 0) { 22 | if (matrix[i][j + 1] === undefined || matrix[i][j + 1] > target) { 23 | return false; 24 | } 25 | j++; 26 | } else if (j === n - 1) { 27 | if (matrix[i - 1][j] === undefined || matrix[i - 1][j] < target) { 28 | return false; 29 | } 30 | i--; 31 | } else { 32 | if (matrix[i - 1][j] < target) { 33 | j++; 34 | } else { 35 | i--; 36 | } 37 | } 38 | } 39 | return false; 40 | 41 | }; -------------------------------------------------------------------------------- /algorithms/95.Unique Binary Search Trees II/Unique Binary Search Trees II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {number} n 10 | * @return {TreeNode[]} 11 | */ 12 | var generateTrees = function (n) { 13 | if (n <= 0) { 14 | return []; 15 | } 16 | return traversal(0, n); 17 | 18 | function traversal(start, end) { 19 | if (start > end) { 20 | return [null]; 21 | } 22 | const result = []; 23 | for (let i = start; i <= end; i++) { 24 | const left = traversal(start, i - 1); 25 | const right = traversal(i + 1, end); 26 | for (let j = 0; j < left.length; j++) { 27 | for (let k = 0; k < right.length; k++) { 28 | const root = new TreeNode(i); 29 | root.left = left[j]; 30 | root.right = right[k]; 31 | result.push(root); 32 | } 33 | } 34 | } 35 | return result; 36 | } 37 | }; -------------------------------------------------------------------------------- /algorithms/655.Print Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Print Binary Tree 2 | 3 | 使用一个m * n 的2D数组打印一个二叉树,遵循下面规则: 4 | 5 | 1. 行宽度应该等于给定二叉树的树高 6 | 2. 列数应该总为奇数 7 | 3. 树的根节点应该是第一行的中间位置。根几点所处的行与列应该将这个剩余空间分成两部分(左下部分和右下部分)。你应该打印左子树在左下部分,右子树在右下部分。左下部分和右下部分规模应该一致。如果一个子树为空而另一个子树不为空,你不需要打印不存在的子树但是仍然应该同另一边一样为不存的子树预留空间,然而,如果两棵子树都为空,你就不应该为其预留空间。 8 | 4. 每个不使用的空间应该为空字符串""。 9 | 5. 打印子树也因该遵循以下规则。 10 | 11 | 例1: 12 | 13 | ``` 14 | Input: 15 | 1 16 | / 17 | 2 18 | Output: 19 | [["", "1", ""], 20 | ["2", "", ""]] 21 | ``` 22 | 23 | 例2: 24 | 25 | ``` 26 | Input: 27 | 1 28 | / \ 29 | 2 3 30 | \ 31 | 4 32 | Output: 33 | [["", "", "", "1", "", "", ""], 34 | ["", "2", "", "", "", "3", ""], 35 | ["", "", "4", "", "", "", ""]] 36 | ``` 37 | 38 | 例3: 39 | 40 | ``` 41 | Input: 42 | 1 43 | / \ 44 | 2 5 45 | / 46 | 3 47 | / 48 | 4 49 | Output: 50 | 51 | [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""] 52 | ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""] 53 | ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""] 54 | ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]] 55 | ``` 56 | 57 | 注意: 58 | 59 | 二叉树的高度范围在[0,10] -------------------------------------------------------------------------------- /algorithms/160.Intersection of Two Linked Lists/Intersection of Two Linked Lists.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * @param {ListNode} headA 11 | * @param {ListNode} headB 12 | * @return {ListNode} 13 | */ 14 | var getIntersectionNode = function(headA, headB) { 15 | var getLinkListLength = function (root) { 16 | var length = 0; 17 | while (root){ 18 | length++; 19 | root = root.next; 20 | } 21 | return length 22 | }; 23 | var Alength = getLinkListLength(headA); 24 | var Blength = getLinkListLength(headB); 25 | var diff = Alength - Blength; 26 | 27 | if(diff < 0){ 28 | diff = - diff; 29 | var temp = headB; 30 | headB = headA; 31 | headA = temp; 32 | } 33 | 34 | while (diff > 0 ){ 35 | headA = headA.next; 36 | diff--; 37 | } 38 | 39 | while(headA && headB){ 40 | if(headA ==headB){ 41 | return headA; 42 | } 43 | headA = headA.next; 44 | headB = headB.next; 45 | } 46 | 47 | return null; 48 | }; -------------------------------------------------------------------------------- /algorithms/623.Add One Row to Tree/Add One Row to Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * 迭代版本 10 | * @param {TreeNode} root 11 | * @param {number} v 12 | * @param {number} d 13 | * @return {TreeNode} 14 | */ 15 | var addOneRow = function (root, v, d) { 16 | if (d === 1) { 17 | const node = new TreeNode(v); 18 | node.left = root; 19 | return node; 20 | } 21 | 22 | traversal(root, 1, v, d); 23 | 24 | return root; 25 | 26 | function traversal(root, depth, val, d) { 27 | if(!root || depth > d - 1){ 28 | return; 29 | } 30 | if(depth == d - 1){ 31 | let left = root.left; 32 | let right = root.right; 33 | 34 | root.left = new TreeNode(val); 35 | root.right = new TreeNode(val); 36 | 37 | root.left.left = left; 38 | root.right.right = right; 39 | 40 | }else{ 41 | traversal(root.left, depth + 1, val, d); 42 | traversal(root.right, depth + 1, val, d); 43 | } 44 | } 45 | 46 | 47 | }; -------------------------------------------------------------------------------- /algorithms/257.Binary Tree Paths/Binary Tree Paths.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {string[]} 11 | */ 12 | var binaryTreePaths = function (root) { 13 | 14 | const path = []; 15 | const cache = []; 16 | traversal(root, cache, path); 17 | return path; 18 | 19 | function traversal(root, cache, path) { 20 | if (!root) { 21 | return null; 22 | } 23 | cache.push(root.val); 24 | if (!root.left && !root.right) { 25 | path.push(createPath(cache)); 26 | }else { 27 | root.left && traversal(root.left, cache, path); 28 | root.right && traversal(root.right, cache, path); 29 | } 30 | cache.pop(); 31 | } 32 | 33 | 34 | function createPath(array) { 35 | let string = ''; 36 | for(let i= 0; i< array.length; i++){ 37 | string += array[i]; 38 | if(i != array.length - 1){ 39 | string += '->' 40 | } 41 | } 42 | return string; 43 | } 44 | 45 | }; -------------------------------------------------------------------------------- /algorithms/74.Search a 2D Matrix/Search a 2D Matrix.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @param {number} target 4 | * @return {boolean} 5 | */ 6 | var searchMatrix = function (matrix, target) { 7 | if (!matrix || matrix.length <= 0 || matrix[0].length <= 0) { 8 | return false; 9 | } 10 | 11 | let m = matrix.length - 1, n = matrix[0].length - 1; 12 | while (m >= 0 && n >= 0) { 13 | 14 | if (matrix[m][n] === target) { 15 | return true; 16 | } 17 | 18 | const left = m >= 0 ? matrix[m][n - 1] : null; 19 | const top = m - 1 >= 0 ? matrix[m - 1][n] : null; 20 | 21 | if (m == 0) { 22 | if (target > left) { 23 | return false; 24 | } 25 | n--; 26 | 27 | 28 | } else if (n == 0) { 29 | if (target > top) { 30 | return false; 31 | } 32 | m--; 33 | 34 | } else { 35 | if (target <= top) { 36 | m--; 37 | } else if (target <= left) { 38 | n--; 39 | } else { 40 | return false; 41 | } 42 | } 43 | } 44 | return false; 45 | }; -------------------------------------------------------------------------------- /algorithms/2.Add Two Numbers/Add Two Numbers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} l1 10 | * @param {ListNode} l2 11 | * @return {ListNode} 12 | */ 13 | var addTwoNumbers = function(l1, l2) { 14 | var pos1 = l1; 15 | var pos2 = l2; 16 | var virtualHead = new ListNode(-1); 17 | var pos = virtualHead; 18 | while(pos1 && pos2){ 19 | var temp = new ListNode(pos1.val + pos2.val); 20 | pos.next = temp; 21 | pos = temp; 22 | pos1 = pos1.next; 23 | pos2 = pos2.next; 24 | } 25 | if(pos2){ 26 | pos1 = pos2; 27 | } 28 | 29 | while (pos1){ 30 | var temp = new ListNode(pos1.val); 31 | pos.next = temp; 32 | pos = temp; 33 | pos1 = pos1.next; 34 | } 35 | 36 | pos = virtualHead.next; 37 | while(pos){ 38 | if(pos.val >= 10){ 39 | pos.next ? 40 | pos.next.val += Math.floor(pos.val / 10): 41 | pos.next = new ListNode(Math.floor(pos.val / 10)); 42 | pos.val %= 10; 43 | } 44 | pos = pos.next; 45 | } 46 | return virtualHead.next; 47 | }; -------------------------------------------------------------------------------- /algorithms/229.Majority Element II/Majority Element II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var majorityElement = function (nums) { 6 | 7 | let majority1 = null, majority2 = null; 8 | let majority1Count = 0, majority2Count = 0; 9 | const result = []; 10 | 11 | nums.forEach((value) => { 12 | 13 | if (value === majority1) { 14 | majority1Count++; 15 | } else if (value === majority2) { 16 | majority2Count++; 17 | } else if (majority1Count === 0) { 18 | majority1 = value; 19 | majority1Count++; 20 | } else if (majority2Count === 0) { 21 | majority2 = value; 22 | majority2Count++; 23 | } else { 24 | majority1Count--; 25 | majority2Count--; 26 | } 27 | }); 28 | 29 | 30 | majority1Count = 0, majority2Count = 0; 31 | 32 | nums.forEach((value)=>{ 33 | if(value === majority1) majority1Count ++; 34 | if(value === majority2) majority2Count ++; 35 | }); 36 | 37 | if(majority1Count > Math.floor(nums.length / 3)) result.push(majority1); 38 | if(majority2Count > Math.floor(nums.length / 3)) result.push(majority2); 39 | 40 | return result; 41 | }; -------------------------------------------------------------------------------- /algorithms/508.Most Frequent Subtree Sum/Most Frequent Subtree Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[]} 11 | */ 12 | var findFrequentTreeSum = function (root) { 13 | 14 | let frequencyMap = new Map(); 15 | 16 | traversal(root, frequencyMap); 17 | 18 | 19 | let maxCount = 0; 20 | let maxArray = []; 21 | 22 | for (let [key, value] of frequencyMap) { 23 | if(value > maxCount){ 24 | maxCount = value; 25 | maxArray = [key]; 26 | }else if(maxCount === value){ 27 | maxArray.push(key); 28 | 29 | } 30 | } 31 | 32 | return maxArray; 33 | 34 | function traversal(root, map) { 35 | if (!root) { 36 | return 0; 37 | } 38 | 39 | let left = traversal(root.left, map); 40 | let right = traversal(root.right, map); 41 | const val = left + right + root.val; 42 | if (map.has(val)) { 43 | map.set(val, map.get(val) + 1); 44 | } else { 45 | map.set(val, 1); 46 | } 47 | return val; 48 | } 49 | 50 | }; -------------------------------------------------------------------------------- /algorithms/144.Binary Tree Preorder Traversal/Binary Tree Preorder Traversal.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * Recursive version 10 | * @param {TreeNode} root 11 | * @return {number[]} 12 | */ 13 | var preorderTraversal = function (root) { 14 | 15 | const result = []; 16 | (function traversal(node) { 17 | if (!node) { 18 | return; 19 | } 20 | result.push(node.val); 21 | traversal(node.left); 22 | traversal(node.right); 23 | })(root); 24 | return result; 25 | 26 | }; 27 | 28 | function TreeNode(val) { 29 | this.val = val; 30 | this.left = this.right = null; 31 | } 32 | 33 | /** 34 | * Iterative version 35 | * @param {TreeNode} root 36 | * @return {number[]} 37 | */ 38 | var preorderTraversal = function (root) { 39 | const result = [], stack = []; 40 | let cur = root; 41 | 42 | while (cur || stack.length > 0) { 43 | if (!cur) { 44 | cur = stack.pop(); 45 | } 46 | 47 | result.push(cur.val); 48 | cur.right && stack.push(cur.right); 49 | cur = cur.left; 50 | } 51 | return result; 52 | }; -------------------------------------------------------------------------------- /algorithms/662.Maximum Width of Binary Tree/Maximum Width of Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number} 11 | */ 12 | var widthOfBinaryTree = function (root) { 13 | 14 | if (!root) { 15 | return 0; 16 | } 17 | 18 | const queue = [root], numQueue = [1]; 19 | let levelEnd = root, levelStartNum = 1; 20 | let maxWidth = 0; 21 | 22 | while (queue.length > 0) { 23 | const current = queue.shift(); 24 | const number = numQueue.shift(); 25 | 26 | if (current.left) { 27 | queue.push(current.left); 28 | numQueue.push(number * 2); 29 | } 30 | 31 | if (current.right) { 32 | queue.push(current.right); 33 | numQueue.push(number * 2 + 1); 34 | } 35 | 36 | if (current === levelEnd) { 37 | const levelNum = number - levelStartNum + 1; 38 | levelNum > maxWidth && ( maxWidth = levelNum); 39 | levelStartNum = numQueue[0]; 40 | levelEnd = queue[queue.length - 1]; 41 | } 42 | } 43 | 44 | return maxWidth; 45 | 46 | }; -------------------------------------------------------------------------------- /algorithms/287.Find the Duplicate Number/Find the Duplicate Number.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Binary Search 3 | * Time: O(n* logn) 4 | * Space: O(1) 5 | * @param {number[]} nums 6 | * @return {number} 7 | */ 8 | var findDuplicate = function (nums) { 9 | 10 | let start = 1, end = nums.length - 1; 11 | while (start < end) { 12 | let middle = Math.floor((start + end) / 2); 13 | let cnt = 0; 14 | // 计算总数组中有多少个数小于等于中间数 15 | for (let i = 0; i < nums.length; i++) { 16 | if (nums[i] <= middle) { 17 | cnt++; 18 | } 19 | } 20 | 21 | if (cnt <= middle) { 22 | start = middle + 1; 23 | } else { 24 | end = middle; 25 | } 26 | } 27 | return start; 28 | }; 29 | 30 | 31 | 32 | /** 33 | * LinkList 34 | * Time: O(n) 35 | * Space: O(1) 36 | * @param {number[]} nums 37 | * @return {number} 38 | */ 39 | var findDuplicate = function(nums) { 40 | 41 | let low = 0, fast = 0; 42 | 43 | do{ 44 | low = nums[low]; 45 | fast = nums[nums[fast]]; 46 | }while (low != fast); 47 | 48 | 49 | let point = 0; 50 | 51 | while(point != low){ 52 | point = nums[point]; 53 | low = nums[low]; 54 | } 55 | 56 | return point; 57 | 58 | }; -------------------------------------------------------------------------------- /algorithms/662.Maximum Width of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | # Maximum Width of Binary Tree 2 | 3 | 给定一棵二叉树,编写一个函数来得到二叉树的最大宽度。二叉树的宽度是指所有层的最大宽度。二叉树的结构类似于完整二叉树,但是某些几点会为null。 4 | 5 | 每一层的宽度定义为最左和最右非空节点的之前的长度,其中的空节点会被计算进长度。 6 | 7 | 例1: 8 | ``` 9 | Input: 10 | 11 | 1 12 | / \ 13 | 3 2 14 | / \ \ 15 | 5 3 9 16 | 17 | Output: 4 18 | Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9). 19 | 20 | ``` 21 | 22 | 例2: 23 | 24 | ``` 25 | Input: 26 | 27 | 1 28 | / 29 | 3 30 | / \ 31 | 5 3 32 | 33 | Output: 2 34 | Explanation: The maximum width existing in the third level with the length 2 (5,3). 35 | ``` 36 | 37 | 38 | 例3: 39 | 40 | ``` 41 | Input: 42 | 43 | 1 44 | / \ 45 | 3 2 46 | / 47 | 5 48 | 49 | Output: 2 50 | Explanation: The maximum width existing in the second level with the length 2 (3,2). 51 | ``` 52 | 53 | 例四: 54 | 55 | ``` 56 | Input: 57 | 58 | 1 59 | / \ 60 | 3 2 61 | / \ 62 | 5 9 63 | / \ 64 | 6 7 65 | Output: 8 66 | Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7). 67 | ``` -------------------------------------------------------------------------------- /algorithms/117.Populating Next Right Pointers in Each Node II/Populating Next Right Pointers in Each Node II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree with next pointer. 3 | * function TreeLinkNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = this.next = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * @param {TreeLinkNode} root 11 | * @return {void} Do not return anything, modify tree in-place instead. 12 | */ 13 | var connect = function (root) { 14 | 15 | let leftNode = null, pre = null; 16 | let cur = root; 17 | 18 | while (cur) { 19 | 20 | while (cur) { 21 | 22 | if (cur.left != null) { 23 | if (pre != null) { 24 | pre.next = cur.left; 25 | } else { 26 | leftNode = cur.left; 27 | } 28 | pre = cur.left; 29 | } 30 | 31 | if (cur.right != null) { 32 | if (pre != null) { 33 | pre.next = cur.right; 34 | } else { 35 | leftNode = cur.right; 36 | } 37 | pre = cur.right; 38 | } 39 | 40 | cur = cur.next; 41 | } 42 | 43 | cur = leftNode; 44 | pre = leftNode = null; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /algorithms/501.Find Mode in Binary Search Tree/Find Mode in Binary Search Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {number[]} 11 | */ 12 | var findMode = function (root) { 13 | 14 | let maxCount = 0, count = 0, current= null; 15 | const result = []; 16 | 17 | inOrder(root, (node) => { 18 | if(node.val === current){ 19 | count ++; 20 | }else{ 21 | current = node.val; 22 | count = 1; 23 | } 24 | count > maxCount && (maxCount = count); 25 | }); 26 | 27 | count = 0; 28 | current = null; 29 | 30 | inOrder(root, (node) => { 31 | if(node.val === current){ 32 | count ++; 33 | }else{ 34 | current = node.val; 35 | count = 1; 36 | } 37 | 38 | count === maxCount && (result.push(node.val)) 39 | }); 40 | 41 | return result; 42 | 43 | function inOrder(root, callback) { 44 | if (!root) { 45 | return; 46 | } 47 | 48 | inOrder(root.left, callback); 49 | callback(root); 50 | inOrder(root.right, callback) 51 | } 52 | }; -------------------------------------------------------------------------------- /algorithms/1.Two Sum/Two Sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 排序法 3 | * @param {number[]} nums 4 | * @param {number} target 5 | * @return {number[]} 6 | */ 7 | var twoSum = function (nums, target) { 8 | 9 | var sortArray = nums.slice().sort((a, b) => a - b); 10 | 11 | var i = 0, j = sortArray.length - 1; 12 | 13 | while (i < j && sortArray[i] + sortArray[j] != target) { 14 | 15 | const addNum = sortArray[i] + sortArray[j]; 16 | 17 | if (addNum > target) { 18 | j--; 19 | } 20 | 21 | else { 22 | i++; 23 | } 24 | } 25 | 26 | var preIndex = nums.indexOf(sortArray[i]); 27 | var endIndex = nums.indexOf(sortArray[j]); 28 | 29 | if (endIndex === preIndex) { 30 | endIndex = nums.indexOf(sortArray[j], preIndex + 1); 31 | } 32 | 33 | return [preIndex, endIndex]; 34 | }; 35 | 36 | 37 | /** 38 | * 哈希 39 | * @param {number[]} nums 40 | * @param {number} target 41 | * @return {number[]} 42 | */ 43 | var twoSum = function (nums, target) { 44 | 45 | const map = new Map(); 46 | 47 | for (let i = 0; i < nums.length; i++) { 48 | if (map.has(target - nums[i])) { 49 | return [i, map.get(target - nums[i])].sort((a, b) => a - b) 50 | } else { 51 | map.set(nums[i], i); 52 | } 53 | } 54 | }; -------------------------------------------------------------------------------- /algorithms/496.Next Greater Element I/README.md: -------------------------------------------------------------------------------- 1 | # Next Greater Element I 2 | 3 | You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. 4 | 5 | The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number. 6 | 7 | Example 1: 8 | 9 | >Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. 10 | Output: [-1,3,-1] 11 | Explanation: 12 | For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. 13 | For number 1 in the first array, the next greater number for it in the second array is 3. 14 | For number 2 in the first array, there is no next greater number for it in the second array, so output -1. 15 | 16 | Example 2: 17 | 18 | >Input: nums1 = [2,4], nums2 = [1,2,3,4]. 19 | Output: [3,-1] 20 | Explanation: 21 | For number 2 in the first array, the next greater number for it in the second array is 3. 22 | For number 4 in the first array, there is no next greater number for it in the second array, so output -1. 23 | Note: 24 | 25 | 1. All elements in nums1 and nums2 are unique. 26 | 2. The length of both nums1 and nums2 would not exceed 1000. 27 | -------------------------------------------------------------------------------- /algorithms/173.Binary Search Tree Iterator/Binary Search Tree Iterator.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * @constructor 11 | * @param {TreeNode} root - root of the binary search tree 12 | */ 13 | var BSTIterator = function (root) { 14 | this.stack = []; 15 | let point = root; 16 | while (point) { 17 | this.stack.push(point); 18 | point = point.left; 19 | } 20 | }; 21 | 22 | 23 | /** 24 | * @this BSTIterator 25 | * @returns {boolean} - whether we have a next smallest number 26 | */ 27 | BSTIterator.prototype.hasNext = function () { 28 | return this.stack.length <= 0 ? false : true; 29 | }; 30 | 31 | /** 32 | * @this BSTIterator 33 | * @returns {number} - the next smallest number 34 | */ 35 | BSTIterator.prototype.next = function () { 36 | if (!this.hasNext()) { 37 | return -1; 38 | } 39 | 40 | const node = this.stack.pop(); 41 | const val = node.val; 42 | let point = node.right; 43 | while (point) { 44 | this.stack.push(point); 45 | point = point.left; 46 | } 47 | return val; 48 | }; 49 | 50 | /** 51 | * Your BSTIterator will be called like this: 52 | * var i = new BSTIterator(root), a = []; 53 | * while (i.hasNext()) a.push(i.next()); 54 | */ -------------------------------------------------------------------------------- /algorithms/655.Print Binary Tree/Print Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {string[][]} 11 | */ 12 | var printTree = function (root) { 13 | 14 | // 计算宽高 15 | const height = getHeight(root); 16 | const width = Math.pow(2, height) - 1; 17 | 18 | // 生成结果数组 19 | const array = []; 20 | for (let i = 0; i < height; i++) { 21 | const arr = []; 22 | for (let j = 0; j < width; j++) { 23 | arr.push(''); 24 | } 25 | array.push(arr); 26 | } 27 | 28 | //递归 29 | traversal(root, 0, array, 0 , width - 1); 30 | 31 | return array; 32 | 33 | function traversal(root, curHeight, array, left, right) { 34 | if (!root) return; 35 | const middle = (left + right ) / 2 36 | array[curHeight][middle] = root.val.toString(); 37 | 38 | traversal(root.left, curHeight + 1, array, left, middle - 1); 39 | traversal(root.right, curHeight + 1, array, middle + 1, right); 40 | } 41 | 42 | 43 | function getHeight(root) { 44 | 45 | if (!root) { 46 | return 0; 47 | } 48 | 49 | return 1 + Math.max(getHeight(root.left), getHeight(root.right)); 50 | } 51 | } -------------------------------------------------------------------------------- /algorithms/34.Search for a Range/Search for a Range.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | var searchRange = function (nums, target) { 7 | 8 | return [findLeftMost(nums, target), findRightMost(nums, target)]; 9 | 10 | 11 | function findLeftMost(nums, target) { 12 | 13 | let start = 0, end = nums.length - 1; 14 | 15 | while (start <= end) { 16 | 17 | let mid = Math.floor((start + end) / 2); 18 | 19 | if (nums[mid] >= target) { 20 | end = mid - 1; 21 | } else { 22 | start = mid + 1; 23 | } 24 | } 25 | 26 | if(start >= 0 && start < nums.length && nums[start] === target){ 27 | return start; 28 | } 29 | 30 | return -1; 31 | } 32 | 33 | 34 | function findRightMost(nums, target) { 35 | 36 | let start = 0, end = nums.length - 1; 37 | 38 | while (start <= end){ 39 | 40 | let mid = Math.floor((start + end) / 2); 41 | 42 | if (nums[mid] <= target) { 43 | start = mid + 1; 44 | } else{ 45 | end = mid - 1; 46 | } 47 | } 48 | if(end >= 0 && end < nums.length && target === nums[end]){ 49 | return end; 50 | } 51 | 52 | return -1; 53 | } 54 | 55 | }; -------------------------------------------------------------------------------- /algorithms/155.Min Stack/Min Stack.js: -------------------------------------------------------------------------------- 1 | /** 2 | * initialize your data structure here. 3 | */ 4 | var MinStack = function() { 5 | this.dataStack = []; 6 | this.minStack = []; 7 | this.minValue = Infinity; 8 | }; 9 | 10 | /** 11 | * @param {number} x 12 | * @return {void} 13 | */ 14 | MinStack.prototype.push = function(x) { 15 | this.dataStack.push(x); 16 | if(x < this.minValue){ 17 | this.minValue = x; 18 | } 19 | this.minStack.push(this.minValue); 20 | }; 21 | 22 | /** 23 | * @return {void} 24 | */ 25 | MinStack.prototype.pop = function() { 26 | this.dataStack.pop(); 27 | this.minStack.pop(); 28 | if(this.minStack.length > 0){ 29 | this.minValue = this.minStack.pop(); 30 | this.minStack.push(this.minValue); 31 | } else { 32 | this.minValue = Infinity; 33 | } 34 | }; 35 | 36 | /** 37 | * @return {number} 38 | */ 39 | MinStack.prototype.top = function() { 40 | const x = this.dataStack.pop(); 41 | this.dataStack.push(x); 42 | return x; 43 | }; 44 | 45 | /** 46 | * @return {number} 47 | */ 48 | MinStack.prototype.getMin = function() { 49 | return this.minValue; 50 | }; 51 | 52 | /** 53 | * Your MinStack object will be instantiated and called as such: 54 | * var obj = Object.create(MinStack).createNew() 55 | * obj.push(x) 56 | * obj.pop() 57 | * var param_3 = obj.top() 58 | * var param_4 = obj.getMin() 59 | */ -------------------------------------------------------------------------------- /algorithms/54.Spiral Matrix/Spiral Matrix.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @return {number[]} 4 | */ 5 | var spiralOrder = function (matrix) { 6 | 7 | const rs = []; 8 | if (matrix.length <= 0) { 9 | return rs; 10 | } 11 | 12 | let left = 0, top = 0, right = matrix[0].length - 1, bottom = matrix.length - 1; 13 | 14 | while (outRange(left,right,top,bottom)) { 15 | 16 | for(let i = left; i <= right; i++){ 17 | rs.push(matrix[top][i]); 18 | } 19 | 20 | top ++; 21 | 22 | if(!outRange(left,right,top,bottom)){ 23 | break; 24 | } 25 | 26 | for(let i = top; i <= bottom ; i++){ 27 | rs.push(matrix[i][right]); 28 | } 29 | 30 | right --; 31 | 32 | if(!outRange(left,right,top,bottom)){ 33 | break; 34 | } 35 | 36 | for(let i = right ; i >= left ; i--){ 37 | rs.push(matrix[bottom][i]); 38 | } 39 | 40 | bottom --; 41 | 42 | if(!outRange(left,right,top,bottom)){ 43 | break; 44 | } 45 | 46 | for(let i = bottom ; i >= top; i--){ 47 | rs.push(matrix[i][left]) 48 | } 49 | 50 | left ++; 51 | } 52 | 53 | return rs; 54 | 55 | 56 | function outRange(left,right,top,bottom) { 57 | return left <= right && top <= bottom; 58 | } 59 | 60 | }; -------------------------------------------------------------------------------- /algorithms/297.Serialize and Deserialize Binary Tree/Serialize and Deserialize Binary Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * Encodes a tree to a single string. 11 | * 12 | * @param {TreeNode} root 13 | * @return {string} 14 | */ 15 | var serialize = function(root) { 16 | 17 | const treeArray = []; 18 | preOrder(root, treeArray); 19 | return treeArray.join(','); 20 | 21 | function preOrder(root, array) { 22 | if(!root){ 23 | array.push('null'); 24 | return; 25 | } 26 | 27 | array.push(root.val); 28 | preOrder(root.left, array); 29 | preOrder(root.right, array); 30 | } 31 | 32 | }; 33 | 34 | /** 35 | * Decodes your encoded data to tree. 36 | * 37 | * @param {string} data 38 | * @return {TreeNode} 39 | */ 40 | var deserialize = function(data) { 41 | const treeArray = data.split(','); 42 | return preOrder(treeArray); 43 | 44 | function preOrder(array) { 45 | 46 | const string = array.shift(); 47 | const root = string == 'null' ? null : new TreeNode(parseInt(string)); 48 | if(root){ 49 | root.left = preOrder(array); 50 | root.right = preOrder(array); 51 | } 52 | return root; 53 | } 54 | }; 55 | 56 | /** 57 | * Your functions will be called as such: 58 | * deserialize(serialize(root)); 59 | */ -------------------------------------------------------------------------------- /algorithms/652.Find Duplicate Subtrees/Find Duplicate Subtrees.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @return {TreeNode[]} 11 | */ 12 | var findDuplicateSubtrees = function (root) { 13 | 14 | const map = new Map(); 15 | const rs = []; 16 | traversal(root, map, rs); 17 | return rs; 18 | 19 | function traversal(root, map, result) { 20 | 21 | if(!root){ 22 | return; 23 | } 24 | const rootStr = serialize(root); 25 | const value = map.get(rootStr); 26 | 27 | if (!map.has(rootStr)) { 28 | map.set(rootStr, 1); 29 | 30 | } else { 31 | if (value === 1) { 32 | result.push(root); 33 | } 34 | map.set(rootStr, value + 1); 35 | } 36 | traversal(root.left, map, result); 37 | traversal(root.right, map, result); 38 | } 39 | 40 | /** 41 | * 序列化树结构 42 | */ 43 | function serialize(root) { 44 | 45 | const treeArray = []; 46 | preOrder(root, treeArray); 47 | return treeArray.join(','); 48 | 49 | function preOrder(root, array) { 50 | if (!root) { 51 | array.push('null'); 52 | return; 53 | } 54 | 55 | array.push(root.val); 56 | preOrder(root.left, array); 57 | preOrder(root.right, array); 58 | } 59 | 60 | }; 61 | }; -------------------------------------------------------------------------------- /algorithms/116. Populating Next Right Pointers in Each Node/Populating Next Right Pointers in Each Node.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree with next pointer. 3 | * function TreeLinkNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = this.next = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * 递归版本 11 | * 不太符合题目,使用了递归,存储空间就不是常量了 12 | * @param {TreeLinkNode} root 13 | * @return {void} Do not return anything, modify tree in-place instead. 14 | */ 15 | var connect = function(root) { 16 | 17 | if(!root){ 18 | return; 19 | } 20 | 21 | if(root.left){ 22 | root.left.next = root.right; 23 | } 24 | 25 | if(root.right){ 26 | root.right.next = root.next ? root.next.left : null; 27 | } 28 | 29 | connect(root.left); 30 | connect(root.right); 31 | }; 32 | 33 | 34 | /** 35 | * 迭代版本 36 | * 常量存储空间 37 | * @param {TreeLinkNode} root 38 | * @return {void} Do not return anything, modify tree in-place instead. 39 | */ 40 | var connect = function (root){ 41 | 42 | if(!root){ 43 | return; 44 | } 45 | 46 | var firstLeft = null; 47 | var p = root; 48 | 49 | while (p) { 50 | if (firstLeft == null) { 51 | firstLeft = p.left; 52 | } 53 | 54 | if(p.left){ 55 | p.left.next = p.right; 56 | } 57 | 58 | if(p.right){ 59 | p.right.next = p.next ? p.next.left : null; 60 | } 61 | 62 | if(p.next){ 63 | p = p.next; 64 | }else{ 65 | p = firstLeft; 66 | firstLeft = null; 67 | } 68 | } 69 | }; -------------------------------------------------------------------------------- /algorithms/449.Serialize and Deserialize BST/Serialize and Deserialize BST.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | 9 | /** 10 | * The encoded string should be as compact as possible. 11 | * Encodes a tree to a single string. 12 | * 13 | * @param {TreeNode} root 14 | * @return {string} 15 | */ 16 | var serialize = function (root) { 17 | 18 | var array = []; 19 | preOrder(root, array); 20 | return array.join(','); 21 | 22 | 23 | function preOrder(root, array) { 24 | if (!root) { 25 | return; 26 | } 27 | array.push(root.val); 28 | preOrder(root.left, array); 29 | preOrder(root.right, array); 30 | } 31 | 32 | }; 33 | 34 | /** 35 | * Decodes your encoded data to tree. 36 | * 37 | * @param {string} data 38 | * @return {TreeNode} 39 | */ 40 | var deserialize = function (data) { 41 | if (data === '') { 42 | return null; 43 | } 44 | 45 | const array = data.split(','); 46 | return traversal(array); 47 | 48 | function traversal(array) { 49 | if (array.length <= 0) { 50 | return null; 51 | } 52 | 53 | const val = parseInt(array.shift()); 54 | const node = new TreeNode(val); 55 | let left = array, right = []; 56 | if (array.length > 0) { 57 | for (let i = 0; i < array.length; i++) { 58 | if (array[i] > val) { 59 | left = array.slice(0, i); 60 | right = array.slice(i); 61 | break; 62 | } 63 | } 64 | } 65 | node.left = left.length > 0 ? traversal(left) : null; 66 | node.right = right.length > 0 ? traversal(right) : null; 67 | return node; 68 | } 69 | }; 70 | 71 | /** 72 | * Your functions will be called as such: 73 | * deserialize(serialize(root)); 74 | */ -------------------------------------------------------------------------------- /algorithms/378.Kth Smallest Element in a Sorted Matrix/Kth Smallest Element in a Sorted Matrix.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Binary Search 3 | * @param {number[][]} matrix 4 | * @param {number} k 5 | * @return {number} 6 | */ 7 | var kthSmallest = function (matrix, k) { 8 | 9 | let start = matrix[0][0], end = matrix[matrix.length - 1][matrix[0].length - 1]; 10 | 11 | while (start < end) { 12 | let mid = Math.floor((start + end) / 2); 13 | let count = upper(matrix, mid); 14 | 15 | if (count < k) { 16 | start = mid + 1; 17 | } else { 18 | end = mid 19 | } 20 | } 21 | 22 | return start; 23 | 24 | function upper(matrix, target) { 25 | let count = 0; 26 | for (let i = 0; i < matrix.length; i++) { 27 | let j = 0; 28 | while (j <= matrix[i].length && matrix[i][j] <= target) j++; 29 | count += j; 30 | } 31 | return count; 32 | } 33 | }; 34 | 35 | 36 | /** 37 | * Binary Search optimization 38 | * @param {number[][]} matrix 39 | * @param {number} k 40 | * @return {number} 41 | */ 42 | var kthSmallest = function (matrix, k) { 43 | 44 | let start = matrix[0][0], end = matrix[matrix.length - 1][matrix[0].length - 1]; 45 | 46 | while (start < end) { 47 | let mid = Math.floor((start + end) / 2); 48 | let count = upper(matrix, mid); 49 | 50 | if (count < k) { 51 | start = mid + 1; 52 | } else { 53 | end = mid 54 | } 55 | } 56 | 57 | return start; 58 | 59 | function upper(matrix, target) { 60 | 61 | let n = matrix.length, i = n - 1; 62 | let j = 0; 63 | let count = 0; 64 | 65 | while (i >= 0 && j < n) { 66 | if (matrix[i][j] <= target) { 67 | count += i + 1; 68 | ++j 69 | } else { 70 | --i; 71 | } 72 | 73 | } 74 | return count; 75 | } 76 | }; -------------------------------------------------------------------------------- /algorithms/450.Delete Node in a BST/Delete Node in a BST.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * @param {TreeNode} root 10 | * @param {number} key 11 | * @return {TreeNode} 12 | */ 13 | var deleteNode = function (root, key) { 14 | 15 | const {node, parent} = findNode(root, key); 16 | if (!node) { 17 | //not find or the tree is empty 18 | return root; 19 | } 20 | 21 | if (!node.left && !node.right) { 22 | parent ? setChild(parent, node, null) : root = null; 23 | } else if (!node.left) { 24 | parent ? setChild(parent, node, node.right) : root = root.right; 25 | } else if (!node.right) { 26 | parent ? setChild(parent, node, node.left) : root = root.left; 27 | } else { 28 | let current = node.right, curParent = node; 29 | while (current.left) { 30 | curParent = current; 31 | current = current.left; 32 | } 33 | node.val = current.val; 34 | setChild(curParent, current, current.right); 35 | } 36 | return root; 37 | 38 | 39 | function findNode(root, key) { 40 | let point = root; 41 | let prePoint = null; 42 | while (point != null) { 43 | if (point.val === key) { 44 | break; 45 | } else if (point.val > key) { 46 | prePoint = point; 47 | point = point.left; 48 | } else { 49 | prePoint = point; 50 | point = point.right; 51 | } 52 | } 53 | return { 54 | node: point, 55 | parent: prePoint 56 | } 57 | } 58 | 59 | function setChild(parent, node, newNode) { 60 | 61 | if (parent.left === node) { 62 | parent.left = newNode; 63 | } else { 64 | parent.right = newNode; 65 | } 66 | } 67 | }; -------------------------------------------------------------------------------- /algorithms/101.Symmetric Tree/Symmetric Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * recursively 10 | * @param {TreeNode} root 11 | * @return {boolean} 12 | */ 13 | 14 | 15 | function TreeNode(val) { 16 | this.val = val; 17 | this.left = this.right = null; 18 | } 19 | 20 | var isSymmetric = function (root) { 21 | 22 | if (!root) { 23 | return true; 24 | } 25 | 26 | return checkSymmetric(root.left, root.right); 27 | 28 | function checkSymmetric(left, right) { 29 | if (!left && !right) { 30 | return true; 31 | } 32 | if ((!left && right) || (!right && left) || left.val != right.val) { 33 | return false; 34 | } 35 | 36 | return checkSymmetric(left.left, right.right) && checkSymmetric(left.right, right.left); 37 | } 38 | 39 | }; 40 | 41 | /** 42 | * iteratively 43 | * @param {TreeNode} root 44 | * @return {boolean} 45 | */ 46 | var isSymmetric = function (root) { 47 | 48 | if (!root) { 49 | return true; 50 | } 51 | 52 | const leftQueue = []; 53 | const rightQueue = []; 54 | 55 | leftQueue.push(root.left); 56 | rightQueue.push(root.right); 57 | 58 | while (leftQueue.length > 0 && rightQueue.length > 0) { 59 | const leftNode = leftQueue.shift(); 60 | const rightNode = rightQueue.shift(); 61 | if(!leftNode && !rightNode){ 62 | continue; 63 | } 64 | if ((leftNode && !rightNode) || (!leftNode && rightNode) || (leftNode.val != rightNode.val)) { 65 | return false; 66 | } 67 | leftQueue.push(leftNode.left); 68 | leftQueue.push(leftNode.right); 69 | rightQueue.push(rightNode.right); 70 | rightQueue.push(rightNode.left); 71 | } 72 | if (leftQueue.length > 0 || rightQueue.length > 0) { 73 | return false; 74 | } 75 | return true; 76 | } -------------------------------------------------------------------------------- /algorithms/225.Implement Stack using Queues/Implement Stack using Queues.js: -------------------------------------------------------------------------------- 1 | class Queue { 2 | 3 | constructor() { 4 | this.data = []; 5 | } 6 | 7 | push(x) { 8 | this.data.push(x); 9 | } 10 | 11 | pop() { 12 | if (!this.empty()) { 13 | return this.data.shift(); 14 | } 15 | } 16 | 17 | peek() { 18 | if (!this.empty()) { 19 | return this.data[0]; 20 | } 21 | } 22 | 23 | size() { 24 | return this.data.length; 25 | } 26 | 27 | empty() { 28 | return this.size() <= 0; 29 | } 30 | 31 | } 32 | 33 | 34 | /** 35 | * Initialize your data structure here. 36 | */ 37 | var MyStack = function () { 38 | this.queue1 = new Queue(); 39 | this.queue2 = new Queue(); 40 | }; 41 | 42 | /** 43 | * Push element x onto stack. 44 | * @param {number} x 45 | * @return {void} 46 | */ 47 | MyStack.prototype.push = function (x) { 48 | this.queue1.push(x); 49 | }; 50 | 51 | /** 52 | * Removes the element on top of the stack and returns that element. 53 | * @return {number} 54 | */ 55 | MyStack.prototype.pop = function () { 56 | if (!this.empty()) { 57 | let result; 58 | while (!this.queue1.empty()) { 59 | const x = this.queue1.pop(); 60 | if (!this.queue1.empty()) { 61 | this.queue2.push(x); 62 | } else { 63 | result = x; 64 | } 65 | } 66 | [this.queue1, this.queue2] = [this.queue2, this.queue1]; 67 | return result; 68 | } 69 | }; 70 | 71 | /** 72 | * Get the top element. 73 | * @return {number} 74 | */ 75 | MyStack.prototype.top = function () { 76 | if (!this.empty()) { 77 | let result; 78 | while (!this.queue1.empty()) { 79 | const x = this.queue1.pop(); 80 | if (this.queue1.empty()) { 81 | result = x; 82 | } 83 | this.queue2.push(x); 84 | } 85 | [this.queue1, this.queue2] = [this.queue2, this.queue1]; 86 | return result; 87 | } 88 | }; 89 | 90 | /** 91 | * Returns whether the stack is empty. 92 | * @return {boolean} 93 | */ 94 | MyStack.prototype.empty = function () { 95 | return this.queue1.empty(); 96 | }; 97 | 98 | /** 99 | * Your MyStack object will be instantiated and called as such: 100 | * var obj = Object.create(MyStack).createNew() 101 | * obj.push(x) 102 | * var param_2 = obj.pop() 103 | * var param_3 = obj.top() 104 | * var param_4 = obj.empty() 105 | */ -------------------------------------------------------------------------------- /algorithms/232.Implement Queue using Stacks/Implement Queue using Stacks.js: -------------------------------------------------------------------------------- 1 | class Stack{ 2 | 3 | constructor(){ 4 | this.data = []; 5 | } 6 | 7 | push(element){ 8 | this.data.push(element); 9 | } 10 | 11 | pop(){ 12 | this.data.pop(); 13 | } 14 | 15 | size(){ 16 | return this.data.length; 17 | } 18 | 19 | empty(){ 20 | return this.size() <= 0; 21 | } 22 | 23 | peek(){ 24 | if(!this.empty()){ 25 | const x = this.data.pop(); 26 | this.data.push(x); 27 | return x; 28 | } 29 | } 30 | 31 | } 32 | 33 | /** 34 | * Initialize your data structure here. 35 | */ 36 | var MyQueue = function() { 37 | this.stack1 = new Stack(); 38 | this.stack2 = new Stack(); 39 | }; 40 | 41 | /** 42 | * Push element x to the back of queue. 43 | * @param {number} x 44 | * @return {void} 45 | */ 46 | MyQueue.prototype.push = function(x) { 47 | this.stack1.push(x); 48 | }; 49 | 50 | /** 51 | * Removes the element from in front of queue and returns that element. 52 | * @return {number} 53 | */ 54 | MyQueue.prototype.pop = function() { 55 | if(!this.empty()){ 56 | if(this.stack2.empty() === false){ 57 | const value = this.stack2.peek(); 58 | this.stack2.pop(); 59 | return value; 60 | }else { 61 | while (!this.stack1.empty()){ 62 | this.stack2.push(this.stack1.peek()); 63 | this.stack1.pop(); 64 | } 65 | const value = this.stack2.peek(); 66 | this.stack2.pop(); 67 | return value; 68 | } 69 | } 70 | }; 71 | 72 | /** 73 | * Get the front element. 74 | * @return {number} 75 | */ 76 | MyQueue.prototype.peek = function() { 77 | if(!this.empty()){ 78 | if(this.stack2.empty() === false){ 79 | return this.stack2.peek(); 80 | }else { 81 | while (!this.stack1.empty()){ 82 | this.stack2.push(this.stack1.peek()); 83 | this.stack1.pop(); 84 | } 85 | return this.stack2.peek(); 86 | } 87 | } 88 | }; 89 | 90 | /** 91 | * Returns whether the queue is empty. 92 | * @return {boolean} 93 | */ 94 | MyQueue.prototype.empty = function() { 95 | return this.stack1.empty() && this.stack2.empty(); 96 | }; 97 | 98 | /** 99 | * Your MyQueue object will be instantiated and called as such: 100 | * var obj = Object.create(MyQueue).createNew() 101 | * obj.push(x) 102 | * var param_2 = obj.pop() 103 | * var param_3 = obj.peek() 104 | * var param_4 = obj.empty() 105 | */ -------------------------------------------------------------------------------- /algorithms/94.Binary Tree Inorder Traversal/Binary Tree Inorder Traversal.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * Recursive vision 10 | * @param {TreeNode} root 11 | * @return {number[]} 12 | */ 13 | var inorderTraversal = function(root) { 14 | const rs = []; 15 | traversal(root); 16 | return rs; 17 | 18 | function traversal(root) { 19 | if(!root){ 20 | return; 21 | } 22 | traversal(root.left); 23 | rs.push(root.val); 24 | traversal(root.right); 25 | } 26 | }; 27 | 28 | /** 29 | * Iteratively vision 30 | * @param {TreeNode} root 31 | * @return {number[]} 32 | */ 33 | var inorderTraversal = function(root) { 34 | 35 | let cur = root; 36 | const stack = [], result = []; 37 | 38 | while (cur){ 39 | stack.push(cur); 40 | cur = cur.left 41 | } 42 | 43 | while (stack.length > 0){ 44 | cur = stack.pop(); 45 | result.push(cur.val); 46 | cur = cur.right; 47 | while(cur){ 48 | stack.push(cur); 49 | cur = cur.left 50 | } 51 | } 52 | 53 | return result; 54 | }; 55 | 56 | /** 57 | * time: O(n) 58 | * space: O(1) 59 | * Definition for a binary tree node. 60 | * function TreeNode(val) { 61 | * this.val = val; 62 | * this.left = this.right = null; 63 | * } 64 | */ 65 | /** 66 | * @param {TreeNode} root 67 | * @return {number[]} 68 | */ 69 | var inorderTraversal = function(root) { 70 | 71 | const result = []; 72 | inOrderTraversal(root, (node)=>{ 73 | result.push(node.val); 74 | }); 75 | return result; 76 | 77 | function inOrderTraversal(root ,callback) { 78 | 79 | let current = root; 80 | let inOrderPre = null; 81 | 82 | while (current){ 83 | if(current.left === null){ 84 | callback(current); 85 | current = current.right; 86 | }else{ 87 | //中序遍历的前驱节点 88 | inOrderPre = current.left; 89 | while (inOrderPre.right && inOrderPre.right != current){ 90 | inOrderPre = inOrderPre.right; 91 | } 92 | 93 | if(inOrderPre.right === null){ 94 | inOrderPre.right = current; 95 | current = current.left; 96 | }else{ 97 | inOrderPre.right = null; 98 | callback(current); 99 | current = current.right; 100 | } 101 | } 102 | } 103 | } 104 | }; -------------------------------------------------------------------------------- /algorithms/4.Median of Two Sorted Arrays/Median of Two Sorted Arrays.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums1 3 | * @param {number[]} nums2 4 | * @return {number} 5 | */ 6 | var findMedianSortedArrays = function (nums1, nums2) { 7 | 8 | const length = nums1.length + nums2.length; 9 | 10 | if (length % 2 === 1) { 11 | return findKth(nums1, 0, nums2, 0, Math.floor(length / 2) + 1) 12 | } else { 13 | return (findKth(nums1, 0, nums2, 0, Math.floor(length / 2)) + findKth(nums1, 0, nums2, 0, Math.floor(length / 2 + 1))) / 2; 14 | } 15 | 16 | function findKth(nums1, startA, nums2, startB, k) { 17 | 18 | if (startA >= nums1.length) { 19 | return nums2[startB + k - 1]; 20 | } 21 | 22 | if (startB >= nums2.length) { 23 | return nums1[startA + k - 1] 24 | } 25 | 26 | if (k === 1) { 27 | return Math.min(nums1[startA], nums2[startB]); 28 | } 29 | 30 | const mediumA = startA + Math.floor(k / 2) - 1 < nums1.length ? 31 | nums1[startA + Math.floor(k / 2) - 1] : Number.MAX_VALUE; 32 | 33 | 34 | const mediumB = startB + Math.floor(k / 2) - 1 < nums2.length ? 35 | nums2[startB + Math.floor(k / 2) - 1] : Number.MAX_VALUE; 36 | 37 | if (mediumA < mediumB) { 38 | return findKth(nums1, startA + Math.floor(k / 2), nums2, startB, k - Math.floor(k / 2)); 39 | } else { 40 | return findKth(nums1, startA, nums2, startB + Math.floor(k / 2), k - Math.floor(k / 2)) 41 | } 42 | } 43 | }; 44 | 45 | /** 46 | * 利用slice函数 47 | * @param {number[]} nums1 48 | * @param {number[]} nums2 49 | * @return {number} 50 | */ 51 | 52 | 53 | var findMedianSortedArrays = function (nums1, nums2) { 54 | 55 | const length = nums1.length + nums2.length; 56 | 57 | if (length % 2 === 1) { 58 | return findKth(nums1, nums2, Math.floor(length / 2) + 1) 59 | } else { 60 | return (findKth(nums1, nums2, Math.floor(length / 2)) + findKth(nums1, nums2, Math.floor(length / 2 + 1))) / 2; 61 | } 62 | 63 | function findKth(num1, num2, k) { 64 | if (num1.length <= 0) { 65 | return num2[k - 1]; 66 | } 67 | 68 | if (num2.length <= 0) { 69 | return num1[k - 1]; 70 | } 71 | 72 | if (k === 1) { 73 | return Math.min(num1[0], num2[0]); 74 | } 75 | 76 | const mid = Math.floor(k / 2 - 1 ); 77 | 78 | const medium1 = mid < num1.length ? num1[mid] : Number.MAX_VALUE; 79 | const medium2 = mid < num2.length ? num2[mid] : Number.MAX_VALUE; 80 | 81 | if (medium1 < medium2) { 82 | return findKth(num1.slice(mid + 1), num2, k - mid); 83 | } else { 84 | return findKth(num1, num2.slice(mid + 1), k - mid); 85 | } 86 | } 87 | }; -------------------------------------------------------------------------------- /algorithms/445.Add Two Numbers II/Add Two Numbers II.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 只对Double能表示的数值有效,并不完全满足题意 9 | * @param {ListNode} l1 10 | * @param {ListNode} l2 11 | * @return {ListNode} 12 | */ 13 | var addTwoNumbers = function (l1, l2) { 14 | var num1 = 0; 15 | var num2 = 0; 16 | var pos1 = l1; 17 | var pos2 = l2; 18 | while (pos1) { 19 | num1 = (num1 * 10) + pos1.val; 20 | pos1 = pos1.next; 21 | } 22 | while (pos2) { 23 | num2 = (num2 * 10) + pos2.val; 24 | pos2 = pos2.next; 25 | } 26 | 27 | var num = num1 + num2; 28 | var head = null; 29 | 30 | while (num > 0) { 31 | var digit = new ListNode(num % 10); 32 | digit.next = head; 33 | head = digit; 34 | num = Math.floor((num / 10)); 35 | } 36 | 37 | if (head) { 38 | return head; 39 | } else { 40 | return new ListNode(0); 41 | } 42 | }; 43 | 44 | 45 | /** 46 | * Definition for singly-linked list. 47 | * function ListNode(val) { 48 | * this.val = val; 49 | * this.next = null; 50 | * } 51 | */ 52 | /** 使用数据结构栈,保存两个单链表的数据,然后手动模拟加法过程 53 | * @param {ListNode} l1 54 | * @param {ListNode} l2 55 | * @return {ListNode} 56 | */ 57 | var addTwoNumbers = function (l1, l2) { 58 | var stack1 = []; 59 | var stack2 = []; 60 | var pos1 = l1; 61 | var pos2 = l2; 62 | var head = null; 63 | 64 | while (pos1) { 65 | stack1.push(pos1.val); 66 | pos1 = pos1.next; 67 | } 68 | 69 | while (pos2) { 70 | stack2.push(pos2.val); 71 | pos2 = pos2.next; 72 | } 73 | 74 | while (stack1.length > 0 && stack2.length > 0) { 75 | let num = (stack2.pop()) + (stack1.pop()); 76 | if (num >= 10) { 77 | stack1.length > 0 ? 78 | stack1.push(stack1.pop() + Math.floor(num / 10)): 79 | stack1.push(Math.floor(num / 10)); 80 | num %= 10; 81 | } 82 | digit = new ListNode(num); 83 | digit.next = head; 84 | head = digit; 85 | } 86 | if (stack2.length > 0) { 87 | stack1 = stack2; 88 | } 89 | 90 | while(stack1.length>0){ 91 | let num = stack1.pop(); 92 | if (num >= 10) { 93 | stack1.length > 0 ? 94 | stack1.push(stack1.pop() + Math.floor(num / 10)): 95 | stack1.push(Math.floor(num / 10)); 96 | num %= 10; 97 | } 98 | 99 | let digit = new ListNode(num); 100 | digit.next = head; 101 | head = digit; 102 | } 103 | if(!head){ 104 | return new ListNode(0); 105 | }else { 106 | return head; 107 | } 108 | }; -------------------------------------------------------------------------------- /algorithms/145.Binary Tree Postorder Traversal/Binary Tree Postorder Traversal.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * function TreeNode(val) { 4 | * this.val = val; 5 | * this.left = this.right = null; 6 | * } 7 | */ 8 | /** 9 | * Recursive version 10 | * @param {TreeNode} root 11 | * @return {number[]} 12 | */ 13 | var postorderTraversal = function(root) { 14 | 15 | const result = []; 16 | 17 | (function traversal(node) { 18 | if(!node){ 19 | return; 20 | } 21 | traversal(node.left); 22 | traversal(node.right); 23 | result.push(node.val); 24 | })(root); 25 | return result; 26 | 27 | }; 28 | 29 | 30 | /** 31 | * Definition for a binary tree node. 32 | * function TreeNode(val) { 33 | * this.val = val; 34 | * this.left = this.right = null; 35 | * } 36 | */ 37 | /** 38 | * Iterative version 39 | * @param {TreeNode} root 40 | * @return {number[]} 41 | */ 42 | var postorderTraversal = function(root) { 43 | 44 | class Stack{ 45 | constructor(){ 46 | this.stack = []; 47 | } 48 | 49 | push(val){ 50 | this.stack.push(val); 51 | } 52 | 53 | peak(){ 54 | return this.stack.length > 0 ? this.stack[this.stack.length - 1] : null; 55 | } 56 | 57 | pop(){ 58 | this.stack.pop() 59 | } 60 | 61 | empty(){ 62 | return this.stack.length <= 0; 63 | } 64 | } 65 | 66 | if(!root){ 67 | return []; 68 | } 69 | 70 | const result = []; 71 | const stack = new Stack(); 72 | let preNode = null; 73 | stack.push(root); 74 | 75 | 76 | while (!stack.empty()){ 77 | let cur = stack.peak(); 78 | if((!cur.left && !cur.right) || (preNode && (preNode == cur.left || preNode == cur.right))){ 79 | result.push(cur.val); 80 | stack.pop(); 81 | preNode = cur; 82 | }else{ 83 | cur.right && stack.push(cur.right); 84 | cur.left && stack.push(cur.left); 85 | } 86 | } 87 | return result; 88 | }; 89 | 90 | 91 | var postorderTraversal = function(root) { 92 | 93 | if(!root){ 94 | return []; 95 | } 96 | const stack = [root], flagStack = [false]; 97 | const result = []; 98 | while(stack.length > 0){ 99 | const cur = stack.pop(); 100 | const flag = flagStack.pop(); 101 | 102 | if((!cur.left && !cur.right) || (flag)){ 103 | result.push(cur.val); 104 | }else { 105 | stack.push(cur); 106 | flagStack.push(true); 107 | 108 | if(cur.right){ 109 | stack.push(cur.right); 110 | flagStack.push(false); 111 | } 112 | if(cur.left){ 113 | stack.push(cur.left); 114 | flagStack.push(false); 115 | } 116 | } 117 | } 118 | 119 | return result; 120 | }; -------------------------------------------------------------------------------- /algorithms/99.Recover Binary Search Tree/Recover Binary Search Tree.js: -------------------------------------------------------------------------------- 1 | /** 2 | * space O(n) 3 | * Definition for a binary tree node. 4 | * function TreeNode(val) { 5 | * this.val = val; 6 | * this.left = this.right = null; 7 | * } 8 | */ 9 | /** 10 | * @param {TreeNode} root 11 | * @return {void} Do not return anything, modify root in-place instead. 12 | */ 13 | var recoverTree = function (root) { 14 | 15 | let pre = null; 16 | let cur = null; 17 | let head, last, first = true; 18 | 19 | traversal(root, (node) => { 20 | pre = cur; 21 | cur = node; 22 | 23 | if (pre && cur && pre.val > cur.val) { 24 | if (first) { 25 | head = pre; 26 | first = false; 27 | } 28 | last = cur; 29 | } 30 | }); 31 | 32 | [last.val, head.val] = [head.val, last.val]; 33 | 34 | function traversal(node, callback) { 35 | if (!node) { 36 | return; 37 | } 38 | traversal(node.left, callback); 39 | callback(node); 40 | traversal(node.right, callback); 41 | } 42 | }; 43 | 44 | 45 | /** 46 | * Space O(n) 47 | * Definition for a binary tree node. 48 | * function TreeNode(val) { 49 | * this.val = val; 50 | * this.left = this.right = null; 51 | * } 52 | */ 53 | /** 54 | * @param {TreeNode} root 55 | * @return {void} Do not return anything, modify root in-place instead. 56 | */ 57 | var recoverTree = function (root) { 58 | 59 | let pre = null; 60 | let cur = null; 61 | let head, last, first = true; 62 | 63 | inorderTraversal(root, (node) => { 64 | pre = cur; 65 | cur = node; 66 | 67 | if (pre && cur && pre.val > cur.val) { 68 | if (first) { 69 | head = pre; 70 | first = false; 71 | } 72 | last = cur; 73 | } 74 | }); 75 | 76 | [last.val, head.val] = [head.val, last.val]; 77 | 78 | function inorderTraversal(root, callback) { 79 | let cur = root; 80 | const stack = []; 81 | 82 | while (cur) { 83 | stack.push(cur); 84 | cur = cur.left 85 | } 86 | 87 | while (stack.length > 0) { 88 | cur = stack.pop(); 89 | callback(cur); 90 | cur = cur.right; 91 | while (cur) { 92 | stack.push(cur); 93 | cur = cur.left 94 | } 95 | } 96 | } 97 | }; 98 | 99 | /** 100 | * Space O(1) 101 | * Definition for a binary tree node. 102 | * function TreeNode(val) { 103 | * this.val = val; 104 | * this.left = this.right = null; 105 | * } 106 | */ 107 | /** 108 | * @param {TreeNode} root 109 | * @return {void} Do not return anything, modify root in-place instead. 110 | */ 111 | var recoverTree = function (root) { 112 | 113 | let pre = null; 114 | let cur = null; 115 | let head, last, first = true; 116 | 117 | inorderTraversal(root, (node) => { 118 | pre = cur; 119 | cur = node; 120 | 121 | if (pre && cur && pre.val > cur.val) { 122 | if (first) { 123 | head = pre; 124 | first = false; 125 | } 126 | last = cur; 127 | } 128 | }); 129 | 130 | [last.val, head.val] = [head.val, last.val]; 131 | 132 | function inorderTraversal(root ,callback) { 133 | 134 | let current = root; 135 | let inOrderPre = null; 136 | 137 | while (current){ 138 | if(current.left === null){ 139 | callback(current); 140 | current = current.right; 141 | }else{ 142 | //中序遍历的前驱节点 143 | inOrderPre = current.left; 144 | while (inOrderPre.right && inOrderPre.right != current){ 145 | inOrderPre = inOrderPre.right; 146 | } 147 | 148 | if(inOrderPre.right === null){ 149 | inOrderPre.right = current; 150 | current = current.left; 151 | }else{ 152 | inOrderPre.right = null; 153 | callback(current); 154 | current = current.right; 155 | } 156 | } 157 | } 158 | } 159 | }; 160 | 161 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode答案集 ![Language](https://img.shields.io/badge/Language-JavaScript-orange.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.md) 2 | 3 | ## 关于项目: 4 | 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。 5 | 6 | ## 详情 7 | \# | Title | Solution| Time| Space | Difficulty| 8 | ----|------|---------|-----|-------|-----------| 9 | 1 |Two Sum| JavaScript| O(n * log n) / O(n) | O(1) / O(n)|easy| 10 | 2 |Add Two Numbers| JavaScript| O(n) | O(1)|Medium| 11 | 4 |Median of Two Sorted Arrays| JavaScript| O(log (m+n)) | O(1)|Hard| 12 | 7 |Reverse Integer| JavaScript| O(n) | O(1)|Easy| 13 | 9 |Palindrome Number| JavaScript| O(n) | O(1)|Easy| 14 | 19 |Remove Nth Node From End of List| JavaScript| O(n) | O(1)|Medium| 15 | 21 |Merge Two Sorted Lists| JavaScript| O(n) | O(1)|Easy| 16 | 26 |Remove Duplicates from Sorted Array | JavaScript| O(n) | O(1)|Easy| 17 | 27 |Remove Element| JavaScript| O(n) | O(1)|Easy| 18 | 33 |Search in Rotated Sorted Array| JavaScript|O(log n)|O(1)|Medium| 19 | 34 |Search for a Range| JavaScript|O(log n)|O(1)|Medium| 20 | 35 |Search Insert Position| JavaScript|O(log n)|O(1)|Easy| 21 | 54 |Spiral Matrix| JavaScript| O(n * m) | O(1)|Medium| 22 | 61 |Rotate List| JavaScript| O(n + m) | O(1)|Medium| 23 | 74 |Search a 2D Matrix| JavaScript| O(n ^ 2) | O(n)|Medium| 24 | 78 |Subsets| JavaScript| O(n) | O(1)|Medium| 25 | 83 |Remove Duplicates from Sorted List| JavaScript| O(n) | O(n)|Easy| 26 | 92 |Reverse Linked List II| JavaScript| O(n) | O(1)|Medium| 27 | 94 |Binary Tree Inorder Traversal| JavaScript| O(n) | O(1)|Medium| 28 | 95 |Unique Binary Search Trees II| JavaScript| O(Subsets) | O(n)|Medium| 29 | 96 |Unique Binary Search Trees| JavaScript| O(n ^ 2) | O(n)|Medium| 30 | 98 |Validate Binary Search Tree| JavaScript| O(n) | O(n)|Medium| 31 | 99 |Recover Binary Search Tree| JavaScript| O(n) | O(1)|Hard| 32 | 100|Same Tree| JavaScript| O(n) | O(n)|Easy| 33 | 101|Symmetric Tree| JavaScript| O(n) | O(n)|Easy| 34 | 102|Binary Tree Level Order Traversal| JavaScript| O(n) | O(n)|Medium| 35 | 103|Binary Tree Zigzag Level Order Traversal| JavaScript| O(n) | O(n)|Medium| 36 | 104|Maximum Depth of Binary Tree| JavaScript| O(n) | O(n)|Easy| 37 | 105|Construct Binary Tree from Preorder and Inorder Traversal| JavaScript| O(n) | O(n)|Medium| 38 | 106|Construct Binary Tree from Inorder and Postorder Traversal| JavaScript| O(n) | O(n)|Medium| 39 | 107|Binary Tree Level Order Traversal II| JavaScript| O(n) | O(n)|Easy| 40 | 108|Convert Sorted Array to Binary Search Tree| JavaScript| O(n) | O(n)|Easy| 41 | 110|Balanced Binary Tree| JavaScript| O(n) | O(n)|Easy| 42 | 111|Minimum Depth of Binary Tree| JavaScript| O(n) | O(n)|Easy| 43 | 112|Path Sum| JavaScript| O(n) | O(n)|Easy| 44 | 113|Path Sum II| JavaScript| O(n) | O(n)|Medium| 45 | 114|Flatten Binary Tree to Linked List| JavaScript| O(n) | O(n)|Medium| 46 | 116|Populating Next Right Pointers in Each Node| JavaScript| O(n) | O(1)|Medium| 47 | 117|Populating Next Right Pointers in Each Node II| JavaScript| O(n) | O(1)|Medium| 48 | 118|Pascal's Triangle| JavaScript|O(n ^ 2)| O(1)|Easy| 49 | 119|Pascal's Triangle II| JavaScript|O(n ^ 2)| O(1)|Medium| 50 | 124|Binary Tree Maximum Path Sum| JavaScript| O(n) | O(n)|Hard| 51 | 129|Sum Root to Leaf Numbers|JavaScript| O(n) | O(n)|Medium| 52 | 141|Linked List Cycle| JavaScript| O(n) | O(1)|Easy| 53 | 142|Linked List Cycle II| JavaScript| O(n) | O(1)|Medium| 54 | 144|Binary Tree Preorder Traversal| JavaScript| O(n) | O(n)|Medium| 55 | 145|Binary Tree Postorder Traversal| JavaScript| O(n) | O(n)|Hard| 56 | 153|Find Minimum in Rotated Sorted Array| JavaScript| O(log n) | O(1)|Medium| 57 | 154|Find Minimum in Rotated Sorted Array II| JavaScript| O(n) | O(1)|Medium| 58 | 155|Min Stack| JavaScript| O(1) | O(n)|Easy| 59 | 160|Intersection of Two Linked Lists| JavaScript| O(n) | O(1)|Easy| 60 | 167|Two Sum II - Input array is sorted| JavaScript| O(n) | O(1)|Easy| 61 | 169|Majority Element| JavaScript| O(n) | O(1)|Easy| 62 | 173|Binary Search Tree Iterator| JavaScript| O(n) | O(n)|Medium| 63 | 199|Binary Tree Right Side View| JavaScript| O(n) | O(n)|Easy| 64 | 203|Remove Linked List Elements| JavaScript| O(n) | O(1)|Easy| 65 | 206|Reverse Linked List| JavaScript| O(n)/O(n) | O(1)/O(n)|Easy| 66 | 209|Minimum Size Subarray Sum| JavaScript| O(n) | O(1)|Medium| 67 | 217|Contains Duplicate| JavaScript| O(n) | O(n)|Easy| 68 | 219|Contains Duplicate II| JavaScript| O(n) | O(n)|Easy| 69 | 222|Count Complete Tree Nodes| JavaScript| O(n) | O(n)|Medium| 70 | 226|Invert Binary Tree| JavaScript| O(n) | O(n)|Easy| 71 | 229|Majority Element II| JavaScript| O(n) | O(n)|Medium| 72 | 230|Kth Smallest Element in a BST| JavaScript| O(n) | O(n)|Medium| 73 | 232|Implement Queue using Stacks| JavaScript| O(n) | O(n)|Easy| 74 | 235|Implement Stack using Queues| JavaScript| O(n) | O(n)|Easy| 75 | 234|Palindrome Linked List| JavaScript| O(n) | O(1)|Easy| 76 | 235|Lowest Common Ancestor of a Binary Search Tree| JavaScript| O(n) | O(1)|Easy| 77 | 236|Lowest Common Ancestor of a Binary Search Tree| JavaScript| O(n) | O(n)|Medium| 78 | 237|Delete Node in a Linked List| JavaScript| O(1) | O(1)|Easy| 79 | 238|Product of Array Except Self| JavaScript| O(n) | O(1)|Medium| 80 | 240|Search a 2D Matrix II| JavaScript| O(m + n) | O(1)|Medium| 81 | 257|Binary Tree Paths| JavaScript| O(n) | O(n)|Easy| 82 | 268|Missing Number| JavaScript| O(n) | O(1)|Easy| 83 | 283|Move Zeroes| JavaScript|O(n)|O(1)|Easy| 84 | 287|Find the Duplicate Number| JavaScript|O(n * log n) / O(n)|O(1) / O(1)|Medium| 85 | 297|Serialize and Deserialize Binary Tree| JavaScript| O(n) | O(n)|Hard| 86 | 344|Reverse String| JavaScript| O(n) | O(1)|Easy| 87 | 349|Intersection of Two Arrays| JavaScript| O(n) | O(n)|Easy| 88 | 378|Kth Smallest Element in a Sorted Matrix| JavaScript| O(n * log X) | O(1)|Medium| 89 | 404|Sum of Left Leaves| JavaScript| O(n) | O(n)|Easy| 90 | 437|Path Sum III| JavaScript| O(n) | O(n * log n)|Easy| 91 | 442|Find All Duplicates in an Array| JavaScript| O(n) | O(1)|Medium| 92 | 445|Add Two Numbers II| JavaScript| O(n) | O(1)|Medium| 93 | 448|Find All Numbers Disappeared in an Array| JavaScript| O(n) | O(1)|Easy| 94 | 449|Serialize and Deserialize BST| JavaScript| O(n) | O(n)|Medium| 95 | 450|Delete Node in a BST| JavaScript| O(n) | O(1)|Medium| 96 | 461|Hamming Distance| JavaScript| O(1) | O(1)|Easy| 97 | 476|Number Complement| JavaScript| O(1) | O(1)|Easy| 98 | 501|Find Mode in Binary Search Tree| JavaScript| O(n) | O(n)|Easy| 99 | 508|Most Frequent Subtree Sum| JavaScript| O(n) | O(n)|Medium| 100 | 513|Find Bottom Left Tree Value| JavaScript| O(n) | O(n)|Medium| 101 | 515|Find Largest Value in Each Tree Row| JavaScript| O(n) | O(n)|Medium| 102 | 538|Convert BST to Greater Tree| JavaScript| O(n) | O(n)|Easy| 103 | 543|Diameter of Binary Tree| JavaScript| O(n) | O(n)|Easy| 104 | 561|Array Partition I| JavaScript| O(n) | O(n)|Easy| 105 | 563|Binary Tree Tilt| JavaScript| O(n) | O(n)|Easy| 106 | 572|Subtree of Another Tree| JavaScript| O(n ^ 2) | O(n * n)|Easy| 107 | 606|Construct String from Binary Tree| JavaScript| O(n) | O(n)|Easy| 108 | 617|Merge Two Binary Trees| JavaScript| O(n * log n) | O(1)|Easy| 109 | 623|Add One Row to Tree| JavaScript| O(n) | O(n)|Medium| 110 | 637|Average of Levels in Binary Tree| JavaScript| O(n) | O(n)|Easy| 111 | 643|Maximum Average Subarray I| JavaScript| O(n) | O(1)|Easy| 112 | 652|Find Duplicate Subtrees| JavaScript| O(n ^ 2) | O(n ^ 2)|Medium| 113 | 653|Two Sum IV - Input is a BST| JavaScript| O(n) | O(n)|Easy| 114 | 654|Maximum Binary Tree| JavaScript| O(n * log n) | O(n)|Medium| 115 | 655|Print Binary Tree| JavaScript| O(n) | O(n)|Medium| 116 | 662|Maximum Width of Binary Tree| JavaScript| O(n) | O(n)|Medium| 117 | 669|Trim a Binary Search Tree| JavaScript| O(n) | O(n)|Easy| 118 | 671|Second Minimum Node In a Binary Tree| JavaScript| O(n) | O(n)|Easy| 119 | 674|Longest Continuous Increasing Subsequence| JavaScript| O(n) | O(1)|Easy| --------------------------------------------------------------------------------