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

Commit01aba0a

Browse files
authored
Sri Hari: Batch-3/Neetcode-150: Added Golang, Kotlin (neetcode-gh#3720)
* Added Golang, Kotlin* Added Go & Kt, added best solution for LC:215* Added Golang, Kotlin* Added Golang, Kotlin* Added Golang, Kotlin
1 parent9dfd79d commit01aba0a

19 files changed

+4753
-121
lines changed

‎articles/binary-tree-maximum-path-sum.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,91 @@ public class Solution {
205205
}
206206
```
207207

208+
```go
209+
/**
210+
* Definition for a binary tree node.
211+
* type TreeNode struct {
212+
* Val int
213+
* Left *TreeNode
214+
* Right *TreeNode
215+
* }
216+
*/
217+
funcmaxPathSum(root *TreeNode)int {
218+
res:= -1 <<31
219+
dfs(root, &res)
220+
return res
221+
}
222+
223+
funcdfs(root *TreeNode,res *int) {
224+
if root ==nil {
225+
return
226+
}
227+
left:=getMax(root.Left)
228+
right:=getMax(root.Right)
229+
*res =max(*res, root.Val + left + right)
230+
dfs(root.Left, res)
231+
dfs(root.Right, res)
232+
}
233+
234+
funcgetMax(root *TreeNode)int {
235+
if root ==nil {
236+
return0
237+
}
238+
left:=getMax(root.Left)
239+
right:=getMax(root.Right)
240+
path:= root.Val +max(left, right)
241+
returnmax(0, path)
242+
}
243+
244+
funcmax(a,bint)int {
245+
if a > b {
246+
return a
247+
}
248+
return b
249+
}
250+
```
251+
252+
```kotlin
253+
/**
254+
* Example:
255+
* var ti = TreeNode(5)
256+
* var v = ti.`val`
257+
* Definition for a binary tree node.
258+
* class TreeNode(var `val`: Int) {
259+
* var left: TreeNode? = null
260+
* var right: TreeNode? = null
261+
* }
262+
*/
263+
classSolution {
264+
privatevar res=Int.MIN_VALUE
265+
266+
funmaxPathSum(root:TreeNode?):Int {
267+
dfs(root)
268+
return res
269+
}
270+
271+
privatefundfs(root:TreeNode?) {
272+
if (root==null)return
273+
274+
val left= getMax(root.left)
275+
val right= getMax(root.right)
276+
res= maxOf(res, root.`val`+ left+ right)
277+
278+
dfs(root.left)
279+
dfs(root.right)
280+
}
281+
282+
privatefungetMax(root:TreeNode?):Int {
283+
if (root==null)return0
284+
285+
val left= getMax(root.left)
286+
val right= getMax(root.right)
287+
val path= root.`val`+ maxOf(left, right)
288+
return maxOf(0, path)
289+
}
290+
}
291+
```
292+
208293
::tabs-end
209294

210295
###Time & Space Complexity
@@ -400,6 +485,81 @@ public class Solution {
400485
}
401486
```
402487

488+
```go
489+
/**
490+
* Definition for a binary tree node.
491+
* type TreeNode struct {
492+
* Val int
493+
* Left *TreeNode
494+
* Right *TreeNode
495+
* }
496+
*/
497+
funcmaxPathSum(root *TreeNode)int {
498+
res:= []int{root.Val}
499+
500+
vardfsfunc(node *TreeNode)int
501+
dfs =func(node *TreeNode)int {
502+
if node ==nil {
503+
return0
504+
}
505+
506+
leftMax:=dfs(node.Left)
507+
rightMax:=dfs(node.Right)
508+
509+
leftMax =max(leftMax,0)
510+
rightMax =max(rightMax,0)
511+
512+
res[0] =max(res[0], node.Val+leftMax+rightMax)
513+
514+
return node.Val +max(leftMax, rightMax)
515+
}
516+
517+
dfs(root)
518+
return res[0]
519+
}
520+
521+
funcmax(a,bint)int {
522+
if a > b {
523+
return a
524+
}
525+
return b
526+
}
527+
```
528+
529+
```kotlin
530+
/**
531+
* Example:
532+
* var ti = TreeNode(5)
533+
* var v = ti.`val`
534+
* Definition for a binary tree node.
535+
* class TreeNode(var `val`: Int) {
536+
* var left: TreeNode? = null
537+
* var right: TreeNode? = null
538+
* }
539+
*/
540+
classSolution {
541+
privatevar res=Int.MIN_VALUE
542+
543+
funmaxPathSum(root:TreeNode?):Int {
544+
dfs(root)
545+
return res
546+
}
547+
548+
privatefundfs(node:TreeNode?):Int {
549+
if (node==null) {
550+
return0
551+
}
552+
553+
val leftMax= maxOf(dfs(node.left),0)
554+
val rightMax= maxOf(dfs(node.right),0)
555+
556+
res= maxOf(res, node.`val`+ leftMax+ rightMax)
557+
558+
return node.`val`+ maxOf(leftMax, rightMax)
559+
}
560+
}
561+
```
562+
403563
::tabs-end
404564

405565
###Time & Space Complexity

‎articles/clone-graph.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,74 @@ public class Solution {
222222
}
223223
```
224224

225+
```go
226+
/**
227+
* Definition for a Node.
228+
* type Node struct {
229+
* Val int
230+
* Neighbors []*Node
231+
* }
232+
*/
233+
234+
funccloneGraph(node *Node) *Node {
235+
oldToNew:=make(map[*Node]*Node)
236+
237+
vardfsfunc(*Node) *Node
238+
dfs =func(node *Node) *Node {
239+
if node ==nil {
240+
returnnil
241+
}
242+
243+
if_,found:= oldToNew[node]; found {
244+
return oldToNew[node]
245+
}
246+
247+
copy:= &Node{Val: node.Val}
248+
oldToNew[node] = copy
249+
for_,nei:=range node.Neighbors {
250+
copy.Neighbors =append(copy.Neighbors,dfs(nei))
251+
}
252+
return copy
253+
}
254+
255+
returndfs(node)
256+
}
257+
```
258+
259+
```kotlin
260+
/**
261+
* Definition for a Node.
262+
* class Node(var `val`: Int) {
263+
* var neighbors: ArrayList<Node?> = ArrayList<Node?>()
264+
* }
265+
*/
266+
267+
classSolution {
268+
funcloneGraph(node:Node?):Node? {
269+
if (node==null)returnnull
270+
271+
val oldToNew=HashMap<Node,Node>()
272+
273+
fundfs(node:Node):Node {
274+
if (nodein oldToNew) {
275+
return oldToNew[node]!!
276+
}
277+
278+
val copy=Node(node.`val`)
279+
oldToNew[node]= copy
280+
281+
for (neiin node.neighbors) {
282+
nei?.let { copy.neighbors.add(dfs(it)) }
283+
}
284+
285+
return copy
286+
}
287+
288+
return dfs(node)
289+
}
290+
}
291+
```
292+
225293
::tabs-end
226294

227295
###Time & Space Complexity
@@ -442,6 +510,78 @@ public class Solution {
442510
}
443511
```
444512

513+
```go
514+
/**
515+
* Definition for a Node.
516+
* type Node struct {
517+
* Val int
518+
* Neighbors []*Node
519+
* }
520+
*/
521+
522+
funccloneGraph(node *Node) *Node {
523+
if node ==nil {
524+
returnnil
525+
}
526+
527+
oldToNew:=make(map[*Node]*Node)
528+
oldToNew[node] = &Node{Val: node.Val, Neighbors:make([]*Node,0)}
529+
queue:=make([]*Node,0)
530+
queue =append(queue, node)
531+
532+
forlen(queue) >0 {
533+
cur:= queue[0]
534+
queue = queue[1:]
535+
536+
for_,nei:=range cur.Neighbors {
537+
if_,exists:= oldToNew[nei]; !exists {
538+
oldToNew[nei] = &Node{Val: nei.Val, Neighbors:make([]*Node,0)}
539+
queue =append(queue, nei)
540+
}
541+
oldToNew[cur].Neighbors =append(oldToNew[cur].Neighbors, oldToNew[nei])
542+
}
543+
}
544+
545+
return oldToNew[node]
546+
}
547+
```
548+
549+
```kotlin
550+
/**
551+
* Definition for a Node.
552+
* class Node(var `val`: Int) {
553+
* var neighbors: ArrayList<Node?> = ArrayList<Node?>()
554+
* }
555+
*/
556+
557+
classSolution {
558+
funcloneGraph(node:Node?):Node? {
559+
if (node==null)returnnull
560+
561+
val oldToNew=HashMap<Node,Node>()
562+
oldToNew[node]=Node(node.`val`)
563+
val queue=ArrayDeque<Node>()
564+
queue.add(node)
565+
566+
while (queue.isNotEmpty()) {
567+
val cur= queue.removeFirst()
568+
569+
for (neiin cur.neighbors) {
570+
nei?.let { neighbor->
571+
if (neighbor!in oldToNew) {
572+
oldToNew[neighbor]=Node(neighbor.`val`)
573+
queue.add(neighbor)
574+
}
575+
oldToNew[cur]?.neighbors?.add(oldToNew[neighbor])
576+
}
577+
}
578+
}
579+
580+
return oldToNew[node]
581+
}
582+
}
583+
```
584+
445585
::tabs-end
446586

447587
###Time & Space Complexity

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp