Skip to content

Commit c2dd165

Browse files
authoredAug 6, 2023
Merge pull request #2770 from Abe0770/main
2 parents 0ff990a + f02774a commit c2dd165

4 files changed

+243
-0
lines changed
 
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values.
3+
(i.e., from left to right, then right to left for the next level and alternate between).
4+
5+
Ex. Input: root = [3,9,20,null,null,15,7]
6+
Output: [[3],[20,9],[15,7]]
7+
8+
Time : O(N)
9+
Space : O(N)
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* struct TreeNode {
15+
* int val;
16+
* TreeNode *left;
17+
* TreeNode *right;
18+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
20+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
21+
* };
22+
*/
23+
class Solution {
24+
public:
25+
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
26+
vector <vector <int>> v;
27+
if(root == NULL)
28+
return v;
29+
30+
queue <TreeNode *> q;
31+
q.push(root);
32+
bool changeDirection = true;
33+
34+
while(!q.empty()) {
35+
vector <int> v1;
36+
int siz = q.size();
37+
38+
for(int i = 0 ; i < siz ; i++) {
39+
if(changeDirection)
40+
v1.push_back(q.front() -> val);
41+
else
42+
v1.insert(v1.begin(), q.front() -> val);
43+
44+
if(q.front() -> left != NULL)
45+
q.push(q.front() -> left);
46+
if(q.front() -> right != NULL)
47+
q.push(q.front() -> right);
48+
49+
q.pop();
50+
}
51+
changeDirection = !changeDirection;
52+
v.push_back(v1);
53+
}
54+
return v;
55+
}
56+
};
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
3+
4+
struct Node {
5+
int val;
6+
Node *left;
7+
Node *right;
8+
Node *next;
9+
}
10+
11+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
12+
Initially, all next pointers are set to NULL.
13+
14+
Ex. Input: root = [1,2,3,4,5,6,7]
15+
Output: [1,#,2,3,#,4,5,6,7,#]
16+
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node,
17+
just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
18+
19+
Time : O(N)
20+
Space : O(N)
21+
*/
22+
23+
/*
24+
// Definition for a Node.
25+
class Node {
26+
public:
27+
int val;
28+
Node* left;
29+
Node* right;
30+
Node* next;
31+
32+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
33+
34+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
35+
36+
Node(int _val, Node* _left, Node* _right, Node* _next)
37+
: val(_val), left(_left), right(_right), next(_next) {}
38+
};
39+
*/
40+
41+
class Solution {
42+
public:
43+
Node* connect(Node* root) {
44+
if(root == NULL || (root -> right == NULL && root -> left == NULL))
45+
return root;
46+
queue <Node*>q;
47+
q.push(root);
48+
49+
while(!q.empty()) {
50+
int size = q.size();
51+
Node* temp = NULL;
52+
53+
for(int i = 0 ; i < size ; i++) {
54+
Node* front = q.front();
55+
56+
if(temp != NULL)
57+
temp -> next = front;
58+
temp = front;
59+
60+
if(front -> left) {
61+
q.push(front -> left);
62+
q.push(front -> right);
63+
}
64+
q.pop();
65+
}
66+
}
67+
return root;
68+
}
69+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Given a binary tree
3+
4+
struct Node {
5+
int val;
6+
Node *left;
7+
Node *right;
8+
Node *next;
9+
}
10+
11+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
12+
13+
Initially, all next pointers are set to NULL.
14+
15+
Ex. Input: root = [1,2,3,4,5,null,7]
16+
Output: [1,#,2,3,#,4,5,7,#]
17+
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node,
18+
just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
19+
20+
Time : O(N)
21+
Space : O(1)
22+
*/
23+
24+
/*
25+
// Definition for a Node.
26+
class Node {
27+
public:
28+
int val;
29+
Node* left;
30+
Node* right;
31+
Node* next;
32+
33+
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
34+
35+
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
36+
37+
Node(int _val, Node* _left, Node* _right, Node* _next)
38+
: val(_val), left(_left), right(_right), next(_next) {}
39+
};
40+
*/
41+
42+
class Solution {
43+
public:
44+
Node* connect(Node* root) {
45+
if(root == NULL)
46+
return NULL;
47+
Node* parent = root;
48+
49+
while(parent) {
50+
Node* newNode = new Node;
51+
Node* temp = newNode;
52+
53+
while(parent) {
54+
if(parent -> left) {
55+
temp -> next = parent -> left;
56+
temp = temp -> next;
57+
}
58+
if(parent -> right) {
59+
temp -> next = parent -> right;
60+
temp = temp -> next;
61+
}
62+
parent = parent -> next;
63+
}
64+
parent = newNode -> next;
65+
}
66+
return root;
67+
}
68+
};

Diff for: ‎cpp/0637-average-of-levels-in-binary-tree.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Given the root of a binary tree, return the average value of the nodes on each level in the form of an array.
3+
Answers within 10-5 of the actual answer will be accepted.
4+
5+
Ex. Input: root = [3,9,20,null,null,15,7]
6+
Output: [3.00000,14.50000,11.00000]
7+
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
8+
Hence return [3, 14.5, 11].
9+
10+
Time : O(N)
11+
Space : O(N)
12+
*/
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* struct TreeNode {
17+
* int val;
18+
* TreeNode *left;
19+
* TreeNode *right;
20+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
21+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
23+
* };
24+
*/
25+
class Solution {
26+
public:
27+
vector<double> averageOfLevels(TreeNode* root) {
28+
vector <double> v;
29+
if(root == NULL)
30+
return v;
31+
queue <TreeNode *> q;
32+
q.push(root);
33+
34+
while(!q.empty()) {
35+
double sum = 0, count = 0;
36+
int siz = q.size();
37+
for(int i = 0 ; i < siz ; i++) {
38+
sum += q.front() -> val;
39+
if(q.front() -> left)
40+
q.push(q.front() -> left);
41+
if(q.front() -> right)
42+
q.push(q.front() -> right);
43+
++count;
44+
q.pop();
45+
}
46+
v.push_back(sum / count);
47+
}
48+
return v;
49+
}
50+
};

0 commit comments

Comments
 (0)
Please sign in to comment.