From d88f4fc5c78854a79f26f5f9ba63442ac9fb99fc Mon Sep 17 00:00:00 2001 From: Abel Johnson Date: Sun, 6 Aug 2023 22:42:02 +0530 Subject: [PATCH 1/4] Create 0103-binary-tree-zigzag-level-order-traversal.cpp --- ...nary-tree-zigzag-level-order-traversal.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cpp/0103-binary-tree-zigzag-level-order-traversal.cpp diff --git a/cpp/0103-binary-tree-zigzag-level-order-traversal.cpp b/cpp/0103-binary-tree-zigzag-level-order-traversal.cpp new file mode 100644 index 000000000..6a3f4dcf5 --- /dev/null +++ b/cpp/0103-binary-tree-zigzag-level-order-traversal.cpp @@ -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> zigzagLevelOrder(TreeNode* root) { + vector > v; + if(root == NULL) + return v; + + queue q; + q.push(root); + bool changeDirection = true; + + while(!q.empty()) { + vector 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; + } +}; From 70e35c1e853ad3133025dbdc5493b2ae1b042d7e Mon Sep 17 00:00:00 2001 From: Abel Johnson Date: Sun, 6 Aug 2023 23:02:21 +0530 Subject: [PATCH 2/4] Create 0637-average-of-levels-in-binary-tree.cpp --- cpp/0637-average-of-levels-in-binary-tree.cpp | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 cpp/0637-average-of-levels-in-binary-tree.cpp diff --git a/cpp/0637-average-of-levels-in-binary-tree.cpp b/cpp/0637-average-of-levels-in-binary-tree.cpp new file mode 100644 index 000000000..85da4d9aa --- /dev/null +++ b/cpp/0637-average-of-levels-in-binary-tree.cpp @@ -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 averageOfLevels(TreeNode* root) { + vector v; + if(root == NULL) + return v; + queue 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; + } +}; From fa8adade72d5595c4d15194aa795acf85e6d148c Mon Sep 17 00:00:00 2001 From: Abel Johnson Date: Sun, 6 Aug 2023 23:40:02 +0530 Subject: [PATCH 3/4] Create 0116-populating-next-right-pointers-in-each-node.cpp --- ...ating-next-right-pointers-in-each-node.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 cpp/0116-populating-next-right-pointers-in-each-node.cpp diff --git a/cpp/0116-populating-next-right-pointers-in-each-node.cpp b/cpp/0116-populating-next-right-pointers-in-each-node.cpp new file mode 100644 index 000000000..24a47e615 --- /dev/null +++ b/cpp/0116-populating-next-right-pointers-in-each-node.cpp @@ -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 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; + } +}; From f02774a93c464bb1d37d2abdb6850ff3bc7af6b4 Mon Sep 17 00:00:00 2001 From: Abel Johnson Date: Mon, 7 Aug 2023 01:18:56 +0530 Subject: [PATCH 4/4] Create 0117-populating-next-right-pointers-in-each-node-ii.cpp --- ...ng-next-right-pointers-in-each-node-ii.cpp | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cpp/0117-populating-next-right-pointers-in-each-node-ii.cpp diff --git a/cpp/0117-populating-next-right-pointers-in-each-node-ii.cpp b/cpp/0117-populating-next-right-pointers-in-each-node-ii.cpp new file mode 100644 index 000000000..1973145f2 --- /dev/null +++ b/cpp/0117-populating-next-right-pointers-in-each-node-ii.cpp @@ -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; + } +};