File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java.util.Queue;
2
+ import java.util.LinkedList;
3
+
4
+
5
+ public class Solution {
6
+
7
+ /* TreeNode structure
8
+ *
9
+ * class TreeNode<T> {
10
+ T data;
11
+ ArrayList<TreeNode<T>> children;
12
+ TreeNode(T data){
13
+ this.data = data;
14
+ children = new ArrayList<TreeNode<T>>();
15
+ }
16
+ }*/
17
+
18
+ static int a=0;
19
+
20
+ public static void replaceWithDepthValue(TreeNode<Integer> root){
21
+
22
+
23
+
24
+
25
+ Queue<TreeNode<Integer>> queue = new LinkedList<>();
26
+ //added 1st level here
27
+ queue.add(root);
28
+ queue.add(null);
29
+
30
+ while(!queue.isEmpty())
31
+ {
32
+ TreeNode<Integer> frontNode = queue.remove();
33
+
34
+ if(frontNode == null)
35
+ {
36
+ if(queue.isEmpty())
37
+ {
38
+ break;
39
+ }
40
+
41
+ a++;
42
+ queue.add(null);
43
+ }
44
+ else
45
+ {
46
+ frontNode.data=a;
47
+ for(int i=0;i<frontNode.children.size();i++)
48
+ {
49
+ queue.add(frontNode.children.get(i));
50
+ }
51
+
52
+
53
+ }
54
+
55
+ }
56
+
57
+
58
+ }
59
+
60
+
61
+ }
You can’t perform that action at this time.
0 commit comments