File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ template <typename T>
5
+ class TreeNode
6
+ {
7
+ public:
8
+ T val;
9
+ bool isOriginal;
10
+ TreeNode<T> *left;
11
+ TreeNode<T> *right;
12
+
13
+ TreeNode (T val)
14
+ {
15
+ this ->val = val;
16
+ left = NULL ;
17
+ right = NULL ;
18
+ }
19
+ };
20
+
21
+ int nodeLevel (TreeNode<int > *root, int searchedValue)
22
+ {
23
+ // Write your code here.
24
+ queue<pair<TreeNode<int > *, int >> q;
25
+ // q.push(make_pair(root, 1));
26
+ q.push ({root, 1 });
27
+ while (!q.empty ())
28
+ {
29
+ pair<TreeNode<int > *, int > parent = q.front ();
30
+ TreeNode<int > *node = parent.first ;
31
+ int level = parent.second ;
32
+ q.pop ();
33
+
34
+ if (node->val == searchedValue)
35
+ return level;
36
+
37
+ if (node->left )
38
+ q.push ({node->left , level + 1 });
39
+ if (node->right )
40
+ q.push ({node->right , level + 1 });
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments