File tree 3 files changed +121
-0
lines changed
3 files changed +121
-0
lines changed Original file line number Diff line number Diff line change
1
+ // https://practice.geeksforgeeks.org/problems/clone-a-binary-tree/1
1
2
{
2
3
#include < bits/stdc++.h>
3
4
using namespace std ;
Original file line number Diff line number Diff line change
1
+ {
2
+ #Initial Template for Python 3
3
+ if __name__ == "__main__" :
4
+ t = int (input ())
5
+ for i in range (t ):
6
+ n = int (input ())
7
+ print (numberOf2sinRange (n ))
8
+ }
9
+ ''' This is a function problem.You only need to complete the function given below '''
10
+ #User function Template for python3
11
+ def number0f2s (n ):
12
+ curr = str (n )
13
+ return curr .count ('2' )
14
+ #add Code here
15
+
16
+ def numberOf2sinRange (n ):
17
+ res = 0
18
+ for i in range (1 , n + 1 ):
19
+ res += number0f2s (i )
20
+ return res
21
+ #add code here
Original file line number Diff line number Diff line change
1
+ 9 {
2
+ // Initial Template for C++
3
+ #include < bits/stdc++.h>
4
+ using namespace std ;
5
+ /* A binary tree node has data, pointer to left child
6
+ and a pointer to right child */
7
+ struct Node
8
+ {
9
+ int data;
10
+ struct Node * left;
11
+ struct Node * right;
12
+
13
+ Node (int x){
14
+ data = x;
15
+ left = right = NULL ;
16
+ }
17
+ };
18
+ void mirror (struct Node * node);
19
+ /* Helper function to test mirror(). Given a binary
20
+ search tree, print out its data elements in
21
+ increasing sorted order.*/
22
+ void inOrder (struct Node * node)
23
+ {
24
+ if (node == NULL )
25
+ return ;
26
+ inOrder (node->left );
27
+ printf (" %d " , node->data );
28
+ inOrder (node->right );
29
+ }
30
+ /* Driver program to test size function*/
31
+ int main ()
32
+ {
33
+ int t;
34
+ struct Node *child;
35
+ scanf (" %d
36
+ " , &t);
37
+ while (t--)
38
+ {
39
+ map<int , Node*> m;
40
+ int n;
41
+ scanf (" %d" ,&n);
42
+ struct Node *root = NULL ;
43
+ while (n--)
44
+ {
45
+ Node *parent;
46
+ char lr;
47
+ int n1, n2;
48
+ scanf (" %d %d %c" , &n1, &n2, &lr);
49
+ if (m.find (n1) == m.end ())
50
+ {
51
+ parent = new Node (n1);
52
+ m[n1] = parent;
53
+ if (root == NULL )
54
+ root = parent;
55
+ }
56
+ else
57
+ parent = m[n1];
58
+ child = new Node (n2);
59
+ if (lr == ' L' )
60
+ parent->left = child;
61
+ else
62
+ parent->right = child;
63
+ m[n2] = child;
64
+ }
65
+ mirror (root);
66
+ inOrder (root);
67
+ cout << endl;
68
+ }
69
+ return 0 ;
70
+ }
71
+
72
+ }
73
+ /* This is a function problem.You only need to complete the function given below*/
74
+ // function Template for C++
75
+ /* A binary tree node has data, pointer to left child
76
+ and a pointer to right child /
77
+ struct Node
78
+ {
79
+ int data;
80
+ struct Node* left;
81
+ struct Node* right;
82
+
83
+ Node(int x){
84
+ data = x;
85
+ left = right = NULL;
86
+ }
87
+ }; */
88
+ /* Should convert tree to its mirror */
89
+ void mirror (Node* node)
90
+ {
91
+ // Your Code Here
92
+ if (node == NULL )return ;
93
+ if (node->left == NULL && node->right == NULL ) return ;
94
+ mirror (node->left );
95
+ mirror (node->right );
96
+ Node *temp = node->left ;
97
+ node->left = node->right ;
98
+ node->right = temp;
99
+ }
You can’t perform that action at this time.
0 commit comments