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 reverseList(head *ListNode) *ListNode { | |
curr := head | |
var prev *ListNode | |
for curr != nil { | |
next := curr.Next | |
curr.Next = prev | |
prev = curr | |
curr = next | |
} |
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 levelOrder(root *TreeNode) [][]int { | |
if root == nil { | |
return nil | |
} | |
q := []*TreeNode{root} | |
var result [][]int | |
for len(q) != 0 { | |
size := len(q) |
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, "") | |