Skip to content

Create 0103-binary-tree-zigzag-level-order-traversal.cpp #2770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions cpp/0103-binary-tree-zigzag-level-order-traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values.
(i.e., from left to right, then right to left for the next level and alternate between).

Ex. Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]

Time : O(N)
Space : O(N)
*/

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector <vector <int>> v;
if(root == NULL)
return v;

queue <TreeNode *> q;
q.push(root);
bool changeDirection = true;

while(!q.empty()) {
vector <int> v1;
int siz = q.size();

for(int i = 0 ; i < siz ; i++) {
if(changeDirection)
v1.push_back(q.front() -> val);
else
v1.insert(v1.begin(), q.front() -> val);

if(q.front() -> left != NULL)
q.push(q.front() -> left);
if(q.front() -> right != NULL)
q.push(q.front() -> right);

q.pop();
}
changeDirection = !changeDirection;
v.push_back(v1);
}
return v;
}
};
69 changes: 69 additions & 0 deletions cpp/0116-populating-next-right-pointers-in-each-node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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:

struct Node {
int val;
Node *left;
Node *right;
Node *next;
}

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.
Initially, all next pointers are set to NULL.

Ex. Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node,
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.

Time : O(N)
Space : O(N)
*/

/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;

Node() : val(0), left(NULL), right(NULL), next(NULL) {}

Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
Node* connect(Node* root) {
if(root == NULL || (root -> right == NULL && root -> left == NULL))
return root;
queue <Node*>q;
q.push(root);

while(!q.empty()) {
int size = q.size();
Node* temp = NULL;

for(int i = 0 ; i < size ; i++) {
Node* front = q.front();

if(temp != NULL)
temp -> next = front;
temp = front;

if(front -> left) {
q.push(front -> left);
q.push(front -> right);
}
q.pop();
}
}
return root;
}
};
68 changes: 68 additions & 0 deletions cpp/0117-populating-next-right-pointers-in-each-node-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Given a binary tree

struct Node {
int val;
Node *left;
Node *right;
Node *next;
}

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.

Initially, all next pointers are set to NULL.

Ex. Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node,
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.

Time : O(N)
Space : O(1)
*/

/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;

Node() : val(0), left(NULL), right(NULL), next(NULL) {}

Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
Node* connect(Node* root) {
if(root == NULL)
return NULL;
Node* parent = root;

while(parent) {
Node* newNode = new Node;
Node* temp = newNode;

while(parent) {
if(parent -> left) {
temp -> next = parent -> left;
temp = temp -> next;
}
if(parent -> right) {
temp -> next = parent -> right;
temp = temp -> next;
}
parent = parent -> next;
}
parent = newNode -> next;
}
return root;
}
};
50 changes: 50 additions & 0 deletions cpp/0637-average-of-levels-in-binary-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Given the root of a binary tree, return the average value of the nodes on each level in the form of an array.
Answers within 10-5 of the actual answer will be accepted.

Ex. Input: root = [3,9,20,null,null,15,7]
Output: [3.00000,14.50000,11.00000]
Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11.
Hence return [3, 14.5, 11].

Time : O(N)
Space : O(N)
*/

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector <double> v;
if(root == NULL)
return v;
queue <TreeNode *> q;
q.push(root);

while(!q.empty()) {
double sum = 0, count = 0;
int siz = q.size();
for(int i = 0 ; i < siz ; i++) {
sum += q.front() -> val;
if(q.front() -> left)
q.push(q.front() -> left);
if(q.front() -> right)
q.push(q.front() -> right);
++count;
q.pop();
}
v.push_back(sum / count);
}
return v;
}
};