Skip to content

Instantly share code, notes, and snippets.

Jianmin Chen jianminchen

View GitHub Profile
View Leetcode1008_ConstructBSTFromPreOrder.java
// https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/discuss/252479/Simple-Recursive-solution-in-Java-(more-verbose)
public TreeNode bstFromPreorder(int[] preorder) {
return bstFromPreorder(preorder, 0, preorder.length - 1);
}
public TreeNode bstFromPreorder(int[] order, int start, int end) {
if (start > end) {
return null;
}
TreeNode node = new TreeNode(order[start]);
@jianminchen
jianminchen / leetcode1008.cs
Created Mar 10, 2019
I wrote C# code but it took me 52 minutes in weekly contest 127. I need to learn from ranking no. 1 using data structure to simplify the task
View leetcode1008.cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@jianminchen
jianminchen / leetcode1008.cpp
Created Mar 10, 2019
Leetcode 1008 ranking No. 1 weekly contest 127
View leetcode1008.cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@jianminchen
jianminchen / 1004_MaximumConsecutiveOnesIII.cpp
Created Mar 3, 2019
Leetcode weekly contest 126 - source code top ranking no. 4
View 1004_MaximumConsecutiveOnesIII.cpp
class Solution {
public:
int longestOnes(vector<int>& A, int K) {
int fr = 0, cnt = 0, ans = 0;
for (int i = 0; i < A.size(); i++) {
if (A[i] == 0)
cnt++;
while (cnt > K) {
if (A[fr] == 0)
cnt--;
@jianminchen
jianminchen / 996NumberOfSquarefulArrays.cs
Created Feb 27, 2019
996 number of squareful arrays - Leetcode, I wrote one solution based on another casual coder
View 996NumberOfSquarefulArrays.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _996_number_of_squareful_arrays_II
{
class Program
@jianminchen
jianminchen / CountNodeWithOneChild.cs
Created Feb 15, 2019
2015 Microsoft code screen - count binary tree node with one child
View CountNodeWithOneChild.cs
public static int countOneChildNode(Node node)
{
if(node==null) return 0;
return (((node.left!=null)!=(node.right!=null)? 1:0) + countOneChildNode(node.left) + countOneChildNode(node.right));
}
@jianminchen
jianminchen / Leetcode 979 Distribute coins between binary tree
Created Jan 31, 2019
Mock interview Jan 30, 2019 - 10:00 PM - 11:47 PM - 1:06
View Leetcode 979 Distribute coins between binary tree
//post order
leftMoves = getMoves(left) // 1
rightMoves = getMoves(right) // -1
ownMoves = node.val === 0? -1 : node.val // wrong
totalMoves = Math.abs(leftMoves) + Math.abs(rightMoves) // each node will be visted once - //moves from each edge
return ownMoves + leftMoves + rightMoves // definition of recursive function
Example 4:
4
/-2 -1\
@jianminchen
jianminchen / leetcode979DistributeCoinsBetweenBinarytree.js
Created Jan 31, 2019
interviewing.io mock interview Jan. 30, 2019 - 10:00 PM - 11:40 PM
View leetcode979DistributeCoinsBetweenBinarytree.js
// JavaScript source code
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
@jianminchen
jianminchen / 979DistributeCoinsInBinaryTree.cs
Created Jan 22, 2019
979. Distribute Coins in Binary Tree - solution I wrote in the contest on Jan. 19, 2019. The solution is not working
View 979DistributeCoinsInBinaryTree.cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
@jianminchen
jianminchen / 687_LongestUnivaluePath_caseStudy.cs
Created Jan 20, 2019
687 longest univalue path - code is from a casual coder blog
View 687_LongestUnivaluePath_caseStudy.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _687_longest_univalue_path_IV
{
public class TreeNode
{
You can’t perform that action at this time.