Skip to content

Commit f11390e

Browse files
committed
leetcode
1 parent 03d2146 commit f11390e

File tree

4 files changed

+317
-0
lines changed

4 files changed

+317
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
3+
4+
-* 863. All Nodes Distance K in Binary Tree *-
5+
6+
Given the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance k from the target node.
7+
8+
You can return the answer in any order.
9+
10+
11+
12+
Example 1:
13+
14+
15+
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
16+
Output: [7,4,1]
17+
Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
18+
Example 2:
19+
20+
Input: root = [1], target = 1, k = 3
21+
Output: []
22+
23+
24+
Constraints:
25+
26+
The number of nodes in the tree is in the range [1, 500].
27+
0 <= Node.val <= 500
28+
All the values Node.val are unique.
29+
target is the value of one of the nodes in the tree.
30+
0 <= k <= 1000
31+
32+
*/
33+
34+
import 'dart:collection';
35+
36+
class TreeNode {
37+
late int val;
38+
TreeNode? left;
39+
TreeNode? right;
40+
41+
TreeNode(int val) {
42+
this.val = val;
43+
left = null;
44+
right = null;
45+
}
46+
}
47+
48+
class Solution {
49+
void getParentByBFS(Map<TreeNode?, TreeNode?> parent, TreeNode? root) {
50+
final Queue<TreeNode?> q = Queue<TreeNode?>();
51+
q.add(root);
52+
while (q.isNotEmpty) {
53+
int n = q.length;
54+
while (n > 0) {
55+
final TreeNode? current = q.removeFirst();
56+
if (current!.left != null) {
57+
parent[current.left] = current;
58+
q.add(current.left);
59+
}
60+
if (current.right != null) {
61+
parent[current.right] = current;
62+
q.add(current.right);
63+
}
64+
n--;
65+
}
66+
}
67+
}
68+
69+
void getParentByDFS(Map<TreeNode?, TreeNode?> parent, TreeNode? root) {
70+
if (root == null) return;
71+
if (root.left != null) {
72+
parent[root.left] = root;
73+
}
74+
if (root.right != null) {
75+
parent[root.right] = root;
76+
}
77+
getParentByDFS(parent, root.left);
78+
getParentByDFS(parent, root.right);
79+
}
80+
81+
List<int> distanceK(TreeNode? root, TreeNode? target, int k) {
82+
final HashMap<TreeNode?, TreeNode?> parent = HashMap();
83+
84+
// getParentByDFS(parent, root);
85+
86+
// getParentByBFS(parent, root);
87+
88+
final Queue<TreeNode?> q = Queue<TreeNode?>();
89+
final HashMap<TreeNode?, bool> visited = HashMap();
90+
visited[target] = true;
91+
q.add(target);
92+
int lev = 0;
93+
while (q.isNotEmpty) {
94+
int n = q.length;
95+
if (lev == k) break;
96+
lev++;
97+
while (n > 0) {
98+
final TreeNode? current = q.removeFirst();
99+
if (current!.left != null && !visited[current.left]!) {
100+
visited[current.left] = true;
101+
q.add(current.left);
102+
}
103+
if (current.right != null && !visited[current.right]!) {
104+
visited[current.right] = true;
105+
q.add(current.right);
106+
}
107+
if (parent[current] != null && !visited[parent[current]]!) {
108+
visited[parent[current]] = true;
109+
q.add(parent[current]);
110+
}
111+
n--;
112+
}
113+
}
114+
List<int> answer = [];
115+
while (q.isNotEmpty) {
116+
answer.add(q.first!.val);
117+
q.removeFirst();
118+
}
119+
return answer;
120+
}
121+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func parentChild(child *TreeNode, parent *TreeNode, mpp map[*TreeNode]*TreeNode) {
10+
if child == nil {
11+
return
12+
}
13+
if parent == nil {
14+
parent = child
15+
} else {
16+
mpp[child] = parent
17+
}
18+
parentChild(child.Left, child, mpp)
19+
parentChild(child.Right, child, mpp)
20+
}
21+
22+
func distanceK(root *TreeNode, target *TreeNode, k int) []int {
23+
mpp := make(map[*TreeNode]*TreeNode)
24+
parentChild(root, nil, mpp)
25+
q := []*TreeNode{}
26+
visited := make(map[*TreeNode]bool)
27+
ans := []int{}
28+
29+
q = append(q, target)
30+
visited[target] = true
31+
distance := 0
32+
33+
for len(q) > 0 {
34+
size := len(q)
35+
for i := 0; i < size; i++ {
36+
temp := q[0]
37+
q = q[1:]
38+
39+
leftChild := temp.Left
40+
rightChild := temp.Right
41+
42+
if leftChild != nil && !visited[leftChild] {
43+
q = append(q, leftChild)
44+
visited[leftChild] = true
45+
}
46+
47+
if rightChild != nil && !visited[rightChild] {
48+
q = append(q, rightChild)
49+
visited[rightChild] = true
50+
}
51+
52+
if mpp[temp] != nil && !visited[mpp[temp]] {
53+
q = append(q, mpp[temp])
54+
visited[mpp[temp]] = true
55+
}
56+
57+
if distance == k {
58+
ans = append(ans, temp.Val)
59+
}
60+
}
61+
distance++
62+
if distance > k {
63+
break
64+
}
65+
}
66+
return ans
67+
}
68+
69+
// func main() {
70+
// // Test case
71+
// root := &TreeNode{Val: 3}
72+
// root.Left = &TreeNode{Val: 5}
73+
// root.Right = &TreeNode{Val: 1}
74+
// root.Left.Left = &TreeNode{Val: 6}
75+
// root.Left.Right = &TreeNode{Val: 2}
76+
// root.Right.Left = &TreeNode{Val: 0}
77+
// root.Right.Right = &TreeNode{Val: 8}
78+
// root.Left.Right.Left = &TreeNode{Val: 7}
79+
// root.Left.Right.Right = &TreeNode{Val: 4}
80+
81+
// target := root.Left
82+
// k := 2
83+
84+
// result := distanceK(root, target, k)
85+
// fmt.Println(result) // Output: [7, 4]
86+
// }
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# 🔥 DFS + BFS 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
## Disclaimer
4+
5+
## Tree Definition
6+
7+
```dart
8+
class TreeNode {
9+
late int val;
10+
TreeNode? left;
11+
TreeNode? right;
12+
13+
TreeNode(int val) {
14+
this.val = val;
15+
left = null;
16+
right = null;
17+
}
18+
}
19+
```
20+
21+
## BFS
22+
23+
```dart
24+
void getParentByBFS(Map<TreeNode?, TreeNode?> parent, TreeNode? root) {
25+
final Queue<TreeNode?> q = Queue<TreeNode?>();
26+
q.add(root);
27+
while (q.isNotEmpty) {
28+
int n = q.length;
29+
while (n > 0) {
30+
final TreeNode? current = q.removeFirst();
31+
if (current!.left != null) {
32+
parent[current.left] = current;
33+
q.add(current.left);
34+
}
35+
if (current.right != null) {
36+
parent[current.right] = current;
37+
q.add(current.right);
38+
}
39+
n--;
40+
}
41+
}
42+
}
43+
```
44+
45+
## DFS
46+
47+
```dart
48+
void getParentByDFS(Map<TreeNode?, TreeNode?> parent, TreeNode? root) {
49+
if (root == null) return;
50+
if (root.left != null) {
51+
parent[root.left] = root;
52+
}
53+
if (root.right != null) {
54+
parent[root.right] = root;
55+
}
56+
getParentByDFS(parent, root.left);
57+
getParentByDFS(parent, root.right);
58+
}
59+
```
60+
61+
### Solution
62+
63+
```dart
64+
import 'dart:collection';
65+
class Solution {
66+
List<int> distanceK(TreeNode? root, TreeNode? target, int k) {
67+
final HashMap<TreeNode?, TreeNode?> parent = HashMap();
68+
69+
/* Call One of them here: Don't be stupid to Call Both of them */
70+
71+
// getParentByDFS(parent, root);
72+
73+
// getParentByBFS(parent, root);
74+
75+
final Queue<TreeNode?> q = Queue<TreeNode?>();
76+
final HashMap<TreeNode?, bool> visited = HashMap();
77+
visited[target] = true;
78+
q.add(target);
79+
int lev = 0;
80+
while (q.isNotEmpty) {
81+
int n = q.length;
82+
if (lev == k) break;
83+
lev++;
84+
while (n > 0) {
85+
final TreeNode? current = q.removeFirst();
86+
if (current!.left != null && !visited[current.left]!) {
87+
visited[current.left] = true;
88+
q.add(current.left);
89+
}
90+
if (current.right != null && !visited[current.right]!) {
91+
visited[current.right] = true;
92+
q.add(current.right);
93+
}
94+
if (parent[current] != null && !visited[parent[current]]!) {
95+
visited[parent[current]] = true;
96+
q.add(parent[current]);
97+
}
98+
n--;
99+
}
100+
}
101+
List<int> answer = [];
102+
while (q.isNotEmpty) {
103+
answer.add(q.first!.val);
104+
q.removeFirst();
105+
}
106+
return answer;
107+
}
108+
}
109+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
245245
- [**1493.** Longest Subarray of 1's After Deleting One Element](LongestSubarrayOf1SAfterDeletingOneElement/longest_subarray_of_1s_after_deleting_one_element.dart)
246246
- [**2024.** Maximize the Confusion of an Exam](MaximizeTheConfusionOfAnExam/maximize_the_confusion_of_an_exam.dart)
247247
- [**2551.** Put Marbles in Bags](PutMarblesInBags)
248+
- [**863.** All Nodes Distance K in Binary Tree](AllNodesDistanceKInBinaryTree)
248249

249250
## Reach me via
250251

0 commit comments

Comments
 (0)