Search
🥈

Populating Next Right Pointers in Each Node

Created
2021/12/29 04:00
문제 번호
116
카테고리
Tree
Depth-First Search
Breadth-First Search
Binary Tree

Code

제출 날짜
시간
메모리
2021/12/29
4 ms
6.7 MB
// 116. Populating Next Right Pointers in Each Node // // https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ // Definition for a Node. // type Node struct { // Val int // Left *Node // Right *Node // Next *Node // } // connect function connects current node to the next node. // Recursion has been used to connect the next node. // Also, connects if left node has right child and right node has left child. // 1 1 1 // 2 - 3 2 - 3 2 - 3 // 4 5 6 7 4 5 - 6 7 4 - 5 - 6 - 7 func connect(root *Node) *Node { if root == nil { return root } queue := []*Node{root} for len(queue) > 0 { current := queue[0] queue = queue[1:] if current.Left != nil && current.Right != nil { current.Left.Next = current.Right if current.Next != nil { current.Right.Next = current.Next.Left } queue = append(queue, current.Left) queue = append(queue, current.Right) } } return (root) }
Go
복사