Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbee5de7

Browse files
committed
add new files
1 parent9bd74c2 commitbee5de7

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+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funclowestCommonAncestor(root,p,q*TreeNode)*TreeNode {
10+
11+
ifroot==nil {
12+
returnnil
13+
}
14+
15+
forroot!=nil {
16+
ifp.Val>root.Val&&q.Val>root.Val {
17+
root=root.Right
18+
}elseifp.Val<root.Val&&q.Val<root.Val {
19+
root=root.Left
20+
}else {
21+
returnroot
22+
}
23+
}
24+
returnnil
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+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
funclowestCommonAncestor(root,p,q*TreeNode)*TreeNode {
10+
returnhelper(root,p,q)
11+
}
12+
13+
funchelper(root,p,q*TreeNode)*TreeNode {
14+
ifroot==nil {
15+
returnnil
16+
}
17+
left:=helper(root.Left,p,q)
18+
right:=helper(root.Right,p,q)
19+
ifroot==p||root==q {
20+
returnroot
21+
}
22+
ifleft!=nil&&right!=nil {
23+
returnroot
24+
}
25+
26+
ifleft!=nil&&right==nil {
27+
returnleft
28+
}
29+
ifleft==nil&&right!=nil {
30+
returnright
31+
}
32+
returnnil
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+
typeListNodestruct {
4+
Valint
5+
Next*TreeNode
6+
}
7+
8+
funcdeleteNode(node*ListNode) {
9+
ifnode==nil {
10+
return
11+
}
12+
ifnode.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+
typeTreeNodestruct {
6+
Valint
7+
Left*TreeNode
8+
Right*TreeNode
9+
}
10+
11+
funcbinaryTreePaths(root*TreeNode) []string {
12+
s:= []string{}
13+
ifroot==nil {
14+
returns
15+
}
16+
helper(root,strconv.Itoa(root.Val),&s)
17+
returns
18+
}
19+
20+
funchelper(node*TreeNode,pathstring,s*[]string) {
21+
ifnode.Left==nil&&node.Right==nil {
22+
*s=append(*s,path)
23+
}
24+
ifnode.Left!=nil {
25+
helper(node.Left,path+"->"+strconv.Itoa(node.Left.Val),s)
26+
}
27+
ifnode.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+
funcmissingNumber(nums []int)int {
5+
total:=0
6+
for_,v:=rangenums {
7+
total+=v
8+
}
9+
l:=len(nums)
10+
sum:=l* (l+1)/2
11+
returnsum-total
12+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp