File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/shortest-path-in-binary-matrix/
2
+ //
3
+ // algorithms
4
+ // Medium (60.0%)
5
+ // Total Accepted: 3,575
6
+ // Total Submissions: 5,958
7
+ // beats 100.0% of golang submissions
8
+
9
+ package leetcode
10
+
11
+ /**
12
+ * Definition for a binary tree node.
13
+ * type TreeNode struct {
14
+ * Val int
15
+ * Left *TreeNode
16
+ * Right *TreeNode
17
+ * }
18
+ */
19
+ var res []* TreeNode
20
+
21
+ func delNodes (root * TreeNode , to_delete []int ) []* TreeNode {
22
+ res = []* TreeNode {}
23
+ if root == nil {
24
+ return res
25
+ }
26
+
27
+ hashMap := make (map [int ]bool )
28
+ for _ , v := range to_delete {
29
+ hashMap [v ] = true
30
+ }
31
+
32
+ recursive (root , hashMap , nil )
33
+
34
+ return res
35
+ }
36
+
37
+ func recursive (node * TreeNode , hashMap map [int ]bool , parent * TreeNode ) {
38
+ if node == nil {
39
+ return
40
+ }
41
+
42
+ var parentNode * TreeNode
43
+ if ! hashMap [node .Val ] {
44
+ if parent == nil {
45
+ res = append (res , node )
46
+ }
47
+ parentNode = node
48
+ } else {
49
+ if parent != nil {
50
+ if parent .Left == node {
51
+ parent .Left = nil
52
+ } else {
53
+ parent .Right = nil
54
+ }
55
+ }
56
+ }
57
+
58
+ recursive (node .Left , hashMap , parentNode )
59
+ recursive (node .Right , hashMap , parentNode )
60
+ }
You can’t perform that action at this time.
0 commit comments