Skip to content

Instantly share code, notes, and snippets.

View MorrisLaw's full-sized avatar
💭
writing some code

Jeremy L. Morris MorrisLaw

💭
writing some code
View GitHub Profile
@MorrisLaw
MorrisLaw / invert_binary_tree.go
Created June 2, 2022 11:04
Invert Binary Tree - LeetCode 226
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left, root.Right = root.Right, root.Left
invertTree(root.Left)
invertTree(root.Right)
return root
}
@MorrisLaw
MorrisLaw / max_depth.go
Created June 2, 2022 11:02
Maximum Depth of Binary Tree - LeetCode 104
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}
func max(a, b int) int {
if a > b {
return a
@MorrisLaw
MorrisLaw / diameter_of_tree.go
Created June 2, 2022 10:58
Diameter of a Binary Tree - LeetCode 543
func diameterOfBinaryTree(root *TreeNode) int {
diameter := 0
var postorder func(*TreeNode) int
postorder = func(node *TreeNode) int {
if node == nil {
return 0
}
left := postorder(node.Left)
@MorrisLaw
MorrisLaw / balance_binary_tree.go
Created June 2, 2022 10:54
Balanced Binary Tree - LeetCode 110
func isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
heightDiff := int(math.Abs(float64(getHeight(root.Left) - getHeight(root.Right))))
if heightDiff > 1 {
return false
} else {
return isBalanced(root.Left) && isBalanced(root.Right)
@MorrisLaw
MorrisLaw / lowest_common_ancestor.go
Created June 2, 2022 10:53
Lowest Common Ancestor of a Binary Search Tree - LeetCode 235
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
cur := root
for cur != nil {
if p.Val > cur.Val && q.Val > cur.Val {
cur = cur.Right
} else if p.Val < cur.Val && q.Val < cur.Val {
cur = cur.Left
} else {
break
@MorrisLaw
MorrisLaw / same_tree.go
Created June 2, 2022 10:32
Same Tree - LeetCode 100
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil || q == nil {
return p == q
}
return p.Val == q.Val && isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
}
@MorrisLaw
MorrisLaw / subtree_of_tree.go
Created June 2, 2022 10:29
Subtree of Another Tree - LeetCode 572
func isSubtree(root *TreeNode, subRoot *TreeNode) bool {
if root == nil || subRoot == nil {
return root == subRoot
}
if root.Val == subRoot.Val && isSameTree(root, subRoot) {
return true
}
return isSubtree(root.Left, subRoot) || isSubtree(root.Right, subRoot)
}
@MorrisLaw
MorrisLaw / anagram.go
Created May 12, 2022 21:44
validAnagram
// solution with sorting O(nlogn) time complexity
func isAnagram(s string, t string) bool {
sArr := strings.Split(s, "")
sort.Strings(sArr)
tArr := strings.Split(t, "")
sort.Strings(tArr)
s = strings.Join(sArr, "")
t = strings.Join(tArr, "")
@MorrisLaw
MorrisLaw / best_time_to_buy_and_sell_stock.go
Last active June 3, 2022 11:29
Best Time to Buy and Sell Stock - LeetCode 121
func maxProfit(prices []int) int {
var max int
l, r := 0, 1
for r < len(prices) {
if prices[l] < prices[r] {
max = int(math.Max(float64(max), float64(prices[r]-prices[l])))
} else {
l = r
}
@MorrisLaw
MorrisLaw / twosum.go
Last active June 3, 2022 11:27
Two Sum - LeetCode 1
func twoSum(nums []int, target int) []int {
hm := make(map[int]int)
for i, n := range nums {
if j, exists := hm[n]; exists {
return []int{i, j}
}
hm[target-n] = i
}