This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, "") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |