Skip to content

Commit bee5de7

Browse files
committed
add new files
1 parent 9bd74c2 commit bee5de7

5 files changed

+119
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
10+
11+
if root == nil {
12+
return nil
13+
}
14+
15+
for root != nil {
16+
if p.Val > root.Val && q.Val > root.Val {
17+
root = root.Right
18+
} else if p.Val < root.Val && q.Val < root.Val {
19+
root = root.Left
20+
} else {
21+
return root
22+
}
23+
}
24+
return nil
25+
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
10+
return helper(root, p, q)
11+
}
12+
13+
func helper(root, p, q *TreeNode) *TreeNode {
14+
if root == nil {
15+
return nil
16+
}
17+
left := helper(root.Left, p, q)
18+
right := helper(root.Right, p, q)
19+
if root == p || root == q {
20+
return root
21+
}
22+
if left != nil && right != nil {
23+
return root
24+
}
25+
26+
if left != nil && right == nil {
27+
return left
28+
}
29+
if left == nil && right != nil {
30+
return right
31+
}
32+
return nil
33+
}

237. Delete Node in a Linked List.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
type ListNode struct {
4+
Val int
5+
Next *TreeNode
6+
}
7+
8+
func deleteNode(node *ListNode) {
9+
if node == nil {
10+
return
11+
}
12+
if node.Next == nil {
13+
node = nil
14+
}
15+
node.Val = node.Next.Val
16+
node.Next = node.Next.Next
17+
}

257. Binary Tree Paths.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "strconv"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func binaryTreePaths(root *TreeNode) []string {
12+
s := []string{}
13+
if root == nil {
14+
return s
15+
}
16+
helper(root, strconv.Itoa(root.Val), &s)
17+
return s
18+
}
19+
20+
func helper(node *TreeNode, path string, s *[]string) {
21+
if node.Left == nil && node.Right == nil {
22+
*s = append(*s, path)
23+
}
24+
if node.Left != nil {
25+
helper(node.Left, path+"->"+strconv.Itoa(node.Left.Val), s)
26+
}
27+
if node.Right != nil {
28+
helper(node.Right, path+"->"+strconv.Itoa(node.Right.Val), s)
29+
}
30+
31+
}

268. Missing Number.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
//NB
4+
func missingNumber(nums []int) int {
5+
total := 0
6+
for _, v := range nums {
7+
total += v
8+
}
9+
l := len(nums)
10+
sum := l * (l + 1) / 2
11+
return sum - total
12+
}

0 commit comments

Comments
 (0)