Skip to content

Instantly share code, notes, and snippets.

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode {
if root == nil || p == nil {
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type LevelNode struct {
const undefined = (1 << 31) - 1
func majorityElement(nums []int) int {
candidate := undefined
count := 0
for _, num := range nums {
if count == 0 {
candidate = num
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidBST(root *TreeNode) bool {
return checkIsValid(root, -1 << 32, (1 << 32) - 1)
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type OrderNode struct {
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSubtree(s *TreeNode, t *TreeNode) bool {
queue := []*TreeNode{s}
const inf = (1 << 31) - 1
type pair struct {
id int
level int
}
func ladderLength(beginWord string, endWord string, wordList []string) int {
graph := make(map[string][]int)
// a1 + a2 + a3 + .. + an = n * (a1 + an) / 2
// 1 + 2 + 3 + 4 + 5
// 5 * (1 + 5) / 2 = 10
func arrangeCoins(n int) int {
i, j := 1, n + 1
for i < j {
h := int(uint64(i + j) >> 1)
sum := (h * (1 + h)) / 2
var directions = [4][2]int{
{0, 1},
{1, 0},
{0, -1},
{-1, 0},
}
func findWords(board [][]byte, words []string) []string {
rows := len(board)
func numSquares(n int) int {
// we are going to keep the minumum number of squares required to get the corresponding number
// index here represents the desire number
nums := make([]int, n + 1)
// we calculate the minumum number of squares for each number from 1 to n
for i := 1; i <= n; i++ {
// as we have to get the minumum, at the first step we use INF as undefined number
num := (1 << 31) - 1