Author: devangini123

Problem Statement: Given the root of a binary tree, check whether it is a mirror of itself(i.e., symmetric around its center). Examples: Example 1: Input: root = [1,2,2,3,4,4,3] Output: true Example 2: Input: root = [1,2,2,null,3,null,3] Output: false Constraints: The number of nodes in the tree is in the range [1, 1000]. -100 val == right->val) && isMirror(left->left, right->right) && isMirror(left->right, right->left); } bool isSymmetric(TreeNode* root) { return isMirror(root->left, root->right); } bool isMirror(struct TreeNode* left, struct TreeNode* right) { if (!left && !right) return true; if (!left || !right) return false; return (left->val == right->val) && isMirror(left->left, right->right) && isMirror(left->right,…

Read More

Problem Statement: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children. Examples: Example 1: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: The root-to-leaf path with the target sum is shown. Example 2: Input: root = [1,2,3], targetSum = 5 Output: false Explanation: There are two root-to-leaf paths in the tree: (1 –> 2): The sum is 3. (1 –> 3): The sum is 4. There is…

Read More

Problem Statement: Given the root of a binary tree, return its maximum depth. A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Examples: Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Constraints: The number of nodes in the tree is in the range [0, 104]. -100 left), maxDepth(root->right)) + 1; } #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; int max(int a, int b) { return a > b…

Read More

Problem Statement: Given the root of a binary tree, return the level order traversal of its nodes’ values.. (i.e., from left to right, level by level). Examples: Example 1: Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Example 2: Input: root = [1] Output: [[1]] Explanation: Example 3: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 100]. -100

Read More

Problem Statement: Given the root of a binary tree, return the level order traversal of its nodes’ values.. (i.e., from left to right, level by level). Examples: Example 1: Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Example 2: Input: root = [1] Output: [[1]] Explanation: Example 3: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 left); if (curr->right) q.push(curr->right); levelArr.push_back(curr->val); } ans.push_back(levelArr); } return ans; } #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; typedef struct QueueNode {…

Read More

DFS and BFS in Binary Tree: DFS: Depth First Search DFS explores a tree by going as deep as possible along a branch before backtracking. BFS: Breadth First Search Explore the tree level by level, explore all nodes at the current level before moving deeper. DFS vs BFS DFS: Preorder: A B D E C F Inorder: D B E A C F Postorder: D E B F C A BFS: Level Order Traversal: A B C D E F Zig-Zag Traversal: Zig-Zag Traversal: A C B D E F DFS / BFS DFS uses a Stack (LIFO) First Explore…

Read More

🌙 Problem Statement: Given the root of a binary tree, return the postorder traversal of its nodes’ values.. Examples: Example 1: Input: root = [1,null,2,3] Output: [3,2,1] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,6,7,5,2,9,8,3,1] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 right && peek->right != lastVisited) { curr = peek->right; } else { ans.push_back(peek->val); lastVisited = stack.top(); stack.pop(); } } return ans; } #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX 1000 typedef…

Read More

🌙 Problem Statement: Given the root of a binary tree, return the postorder traversal of its nodes’ values.. Examples: Example 1: Input: root = [1,null,2,3] Output: [3,2,1] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,6,7,5,2,9,8,3,1] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 left); if (curr->right) s1.push(curr->right); } vector ans; while (!s2.empty()) { ans.push_back(s2.top()->val); s2.pop(); } return ans; } void postorderTraversal(struct TreeNode* root, int* result, int* returnSize) { if (!root) return; struct TreeNode* s1[1000]; struct…

Read More

🌙 Problem Statement: Given the root of a binary tree, return the inorder traversal of its nodes values. Examples: Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,2,6,5,7,1,3,9,8] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 val); curr = curr->right; } return ans; } struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; int* inorderTraversal(struct TreeNode* root, int* returnSize) { int* ans = malloc(sizeof(int) *…

Read More

🌙 Problem Statement: Given the root of a binary tree, return the preorder traversal of its nodes’ values. Examples: Example 1: Input: root = [1,null,2,3] Output: [1,2,3] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [1,2,4,5,6,7,3,8,9] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 right) s.push(curr->right); if (curr->left) s.push(curr->left); } return ans; } void traversal(struct TreeNode* curr, int* ans, int* idx) { // Assuming TreeNode is defined as: struct TreeNode { int val; struct TreeNode* left;…

Read More