├── Handwritten Notes(Trees) ├── children sum property.pdf ├── lca and max width of bt.pdf ├── recursive traversal of binary tree.pdf ├── iterative tree traversal of binary tree.pdf ├── nodes at a distance k and max value node.pdf ├── bottom view and right-left side view of bt.pdf ├── symmetric check and root to node path of bt.pdf ├── Diameter and zig-zag traversal of a Binary tree.pdf ├── level order and prepostinorder in one traversal.pdf ├── max depth of a Binary Tree and balanced binary trees.pdf ├── min time taken to burn tree and count complete nodes.pdf ├── vertical order traversal and top view of a Binary tree.pdf └── max path sum, identical , boundary traversal of binary tree.pdf ├── Day13 ├── maxvalnode.cpp └── nodesatk.cpp ├── README.md ├── Day5 ├── identicaltrees.cpp ├── maxpathsum.cpp └── boundarytraversal.cpp ├── Day10 ├── symmetric.cpp └── roottonodepath.cpp ├── Day8 ├── rightleftsideviewofbt.cpp └── bottomview.cpp ├── Day6 ├── diameterofbt.cpp └── zigzag.cpp ├── Day2 └── iterativepreorder.cpp ├── Day4 ├── maxdepthofbt.cpp └── balancedbinarytree.cpp ├── Day11 ├── lca.cpp └── maxwidth.cpp ├── Day3 ├── levorder.cpp └── prepostininonetraversal.cpp ├── Day9 ├── levorder.cpp ├── preorderiterative.cpp └── inoditerative.cpp ├── Day15 ├── completenodes.cpp └── burntree.cpp ├── Day12 └── childrensum.cpp ├── Day1 ├── postordertraversalbt.cpp ├── preorder.cpp └── inorder.cpp └── Day7 ├── topviewofbt.cpp └── verticalorder.cpp /Handwritten Notes(Trees)/children sum property.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/children sum property.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/lca and max width of bt.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/lca and max width of bt.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/recursive traversal of binary tree.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/recursive traversal of binary tree.pdf -------------------------------------------------------------------------------- /Day13/maxvalnode.cpp: -------------------------------------------------------------------------------- 1 | int getMax(Node *root){ 2 | if(root==NULL){ 3 | return INT_MIN; 4 | } 5 | else 6 | return max(root->key,getMax(root->left),getMax(root->right)); 7 | -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/iterative tree traversal of binary tree.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/iterative tree traversal of binary tree.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/nodes at a distance k and max value node.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/nodes at a distance k and max value node.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/bottom view and right-left side view of bt.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/bottom view and right-left side view of bt.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/symmetric check and root to node path of bt.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/symmetric check and root to node path of bt.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/Diameter and zig-zag traversal of a Binary tree.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/Diameter and zig-zag traversal of a Binary tree.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/level order and prepostinorder in one traversal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/level order and prepostinorder in one traversal.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/max depth of a Binary Tree and balanced binary trees.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/max depth of a Binary Tree and balanced binary trees.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/min time taken to burn tree and count complete nodes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/min time taken to burn tree and count complete nodes.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/vertical order traversal and top view of a Binary tree.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/vertical order traversal and top view of a Binary tree.pdf -------------------------------------------------------------------------------- /Handwritten Notes(Trees)/max path sum, identical , boundary traversal of binary tree.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aryans1319/Trees/HEAD/Handwritten Notes(Trees)/max path sum, identical , boundary traversal of binary tree.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Trees 2 | Handwritten Notes along with questions based on tree data structure
3 | Following the Striver Tree series and gfg
4 | Follow me on #Linkedin to get regular updates daywise.
5 | Linkedin Profile-https://www.linkedin.com/in/aryans1319/ 6 | -------------------------------------------------------------------------------- /Day5/identicaltrees.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | bool isSameTree(TreeNode* p, TreeNode* q) { 5 | if(!p) return !q; 6 | if(!q) return !p; 7 | return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Day10/symmetric.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool f(TreeNode *root1, TreeNode* root2) { 4 | if(!root1) return !root2; 5 | if(!root2) return !root1; 6 | return (root1->val == root2->val) && f(root1->left, root2->right) && f(root1->right, root2->left); 7 | } 8 | bool isSymmetric(TreeNode* root) { 9 | if(!root) return true; 10 | return f(root->left, root->right); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Day8/rightleftsideviewofbt.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void recursion(TreeNode *root, int level, vector &res) 4 | { 5 | if(root==NULL) return ; 6 | if(res.size()==level) res.push_back(root->val); 7 | recursion(root->right, level+1, res); 8 | recursion(root->left, level+1, res); 9 | } 10 | 11 | vector rightSideView(TreeNode *root) { 12 | vector res; 13 | recursion(root, 0, res); 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Day6/diameterofbt.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int diameterOfBinaryTree(TreeNode* root) { 4 | int diameter = 0; 5 | height(root, diameter); 6 | return diameter; 7 | } 8 | private: 9 | int height(TreeNode* node, int& diameter) { 10 | if (!node) { 11 | return 0; 12 | } 13 | int lh = height(node->left, diameter); 14 | int rh = height(node->right, diameter); 15 | diameter = max(diameter, lh + rh); 16 | return 1 + max(lh, rh); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Day5/maxpathsum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxPathSum(TreeNode* root) { 4 | int maxi = INT_MIN; 5 | maxPathDown(root, maxi); 6 | return maxi; 7 | } 8 | 9 | int maxPathDown(TreeNode* node, int &maxi) { 10 | if (node == NULL) return 0; 11 | int left = max(0, maxPathDown(node->left, maxi)); 12 | int right = max(0, maxPathDown(node->right, maxi)); 13 | maxi = max(maxi, left + right + node->val); 14 | return max(left, right) + node->val; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Day2/iterativepreorder.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public: 4 | vector preorderTraversal(TreeNode* root) { 5 | vector preorder; 6 | if(root == NULL) return preorder; 7 | 8 | stack st; 9 | st.push(root); 10 | while(!st.empty()){ 11 | root = st.top(); 12 | st.pop(); 13 | preorder.push_back(root->val); 14 | if(root->right != NULL){ 15 | st.push(root->right); 16 | } 17 | if(root->left!= NULL){ 18 | st.push(root->left); 19 | } 20 | } 21 | return preorder; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Day4/maxdepthofbt.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | int maxDepth(TreeNode* root) { 15 | if (root==NULL)return 0; 16 | int lh=maxDepth(root->left); 17 | int rh=maxDepth(root->right); 18 | 19 | return 1+max(lh,rh); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Day11/lca.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 4 | //base case 5 | if (root == NULL || root == p || root == q) { 6 | return root; 7 | } 8 | TreeNode* left = lowestCommonAncestor(root->left, p, q); 9 | TreeNode* right = lowestCommonAncestor(root->right, p, q); 10 | 11 | //result 12 | if(left == NULL) { 13 | return right; 14 | } 15 | else if(right == NULL) { 16 | return left; 17 | } 18 | else { //both left and right are not null, we found our result 19 | return root; 20 | } 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Day3/levorder.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> levelOrder(TreeNode* root) { 4 | vector> ans; 5 | if(root == NULL) return ans; 6 | queue q; 7 | q.push(root); 8 | while(!q.empty()) { 9 | int size = q.size(); 10 | vector level; 11 | for(int i = 0;ileft != NULL) q.push(node->left); 15 | if(node->right != NULL) q.push(node->right); 16 | level.push_back(node->val); 17 | } 18 | ans.push_back(level); 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Day9/levorder.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | vector> levelOrder(TreeNode* root) { 5 | vector> ans; 6 | if(root == NULL) return ans; 7 | queue q; 8 | q.push(root); 9 | while(!q.empty()) { 10 | int size = q.size(); 11 | vector level; 12 | for(int i = 0;ileft != NULL) q.push(node->left); 16 | if(node->right != NULL) q.push(node->right); 17 | level.push_back(node->val); 18 | } 19 | ans.push_back(level); 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Day15/completenodes.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countNodes(TreeNode* root) { 4 | if(root == NULL) return 0; 5 | 6 | int lh = findHeightLeft(root); 7 | int rh = findHeightRight(root); 8 | 9 | if(lh == rh) return (1<left) + countNodes(root->right); 12 | } 13 | int findHeightLeft(TreeNode* node) { 14 | int hght = 0; 15 | while(node) { 16 | hght++; 17 | node = node->left; 18 | } 19 | return hght; 20 | } 21 | int findHeightRight(TreeNode* node) { 22 | int hght = 0; 23 | while(node) { 24 | hght++; 25 | node = node->right; 26 | } 27 | return hght; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Day12/childrensum.cpp: -------------------------------------------------------------------------------- 1 | void reorder(BinaryTreeNode < int > * root) { 2 | if(root == NULL) return; 3 | int child = 0; 4 | if(root->left) { 5 | child += root->left->data; 6 | } 7 | if(root->right) { 8 | child += root->right->data; 9 | } 10 | 11 | if(child >= root->data) root->data = child; 12 | else { 13 | if(root->left) root->left->data = root->data; 14 | else if(root->right) root->right->data = root->data; 15 | } 16 | 17 | reorder(root->left); 18 | reorder(root->right); 19 | 20 | int tot = 0; 21 | if(root->left) tot += root->left->data; 22 | if(root->right) tot+= root->right->data; 23 | if(root->left or root->right) root->data = tot; 24 | } 25 | void changeTree(BinaryTreeNode < int > * root) { 26 | reorder(root); 27 | } 28 | -------------------------------------------------------------------------------- /Day1/postordertraversalbt.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void postorder(TreeNode *node, vector&postod){ 15 | if(node==NULL)return; 16 | postorder(node->left,postod); 17 | postorder(node->right,postod); 18 | postod.push_back(node->val); 19 | 20 | } 21 | 22 | vector postorderTraversal(TreeNode* root) { 23 | vectorpostod; 24 | postorder(root,postod); 25 | return postod; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Day1/preorder.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void dfs(TreeNode *node, vector &preorder) { 15 | if(node == NULL) return; 16 | preorder.push_back(node->val); 17 | dfs(node->left, preorder); 18 | 19 | dfs(node->right, preorder); 20 | } 21 | 22 | 23 | vector preorderTraversal(TreeNode* root) { 24 | vector preorder; 25 | dfs(root, preorder); 26 | return preorder; 27 | } 28 | }; -------------------------------------------------------------------------------- /Day8/bottomview.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector bottomView(Node *root) { 4 | vector ans; 5 | if(root == NULL) return ans; 6 | map mpp; 7 | queue> q; 8 | q.push({root, 0}); 9 | while(!q.empty()) { 10 | auto it = q.front(); 11 | q.pop(); 12 | Node* node = it.first; 13 | int line = it.second; 14 | mpp[line] = node->data; 15 | 16 | if(node->left != NULL) { 17 | q.push({node->left, line-1}); 18 | } 19 | if(node->right != NULL) { 20 | q.push({node->right, line + 1}); 21 | } 22 | 23 | } 24 | 25 | for(auto it : mpp) { 26 | ans.push_back(it.second); 27 | } 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Day1/inorder.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | void inorderTraversal(TreeNode *node,vector&inorder){ 15 | if(node==NULL)return; 16 | inorderTraversal(node->left,inorder); 17 | inorder.push_back(node->val); 18 | inorderTraversal(node->right,inorder); 19 | } 20 | 21 | 22 | vector inorderTraversal(TreeNode* root) { 23 | vectorinorder; 24 | inorderTraversal(root,inorder); 25 | return inorder; 26 | } 27 | }; -------------------------------------------------------------------------------- /Day9/preorderiterative.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector preorderTraversal(TreeNode* root) { 15 | vectorpreod; 16 | if(root==NULL)return preod; 17 | stacks; 18 | s.push(root); 19 | 20 | while(!s.empty()){ 21 | root=s.top(); 22 | s.pop(); 23 | preod.push_back(root->val); 24 | if(root->right!=NULL)s.push(root->right); 25 | if(root->left!=NULL)s.push(root->left); 26 | 27 | } 28 | return preod; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Day11/maxwidth.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int widthOfBinaryTree(TreeNode* root) { 4 | if(!root) 5 | return 0; 6 | int ans = 0; 7 | queue> q; 8 | q.push({root,0}); 9 | while(!q.empty()){ 10 | int size = q.size(); 11 | int mmin = q.front().second; //to make the id starting from zero 12 | int first,last; 13 | for(int i=0; ileft) 20 | q.push({node->left, cur_id*2+1}); 21 | if(node->right) 22 | q.push({node->right, cur_id*2+2}); 23 | } 24 | ans = max(ans, last-first+1); 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Day4/balancedbinarytree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isBalanced(TreeNode* root) { 15 | return dfsHeight(root)!=-1; 16 | } 17 | 18 | int dfsHeight (TreeNode *root) { 19 | if (root == NULL) return 0; 20 | 21 | int leftHeight = dfsHeight (root -> left); 22 | if (leftHeight == -1) return -1; 23 | int rightHeight = dfsHeight (root -> right); 24 | if (rightHeight == -1) return -1; 25 | 26 | if (abs(leftHeight - rightHeight) > 1) return -1; 27 | return max (leftHeight, rightHeight) + 1; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Day9/inoditerative.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | vector inorderTraversal(TreeNode* root) { 15 | stacks; 16 | TreeNode *node=root; 17 | 18 | vectorin; 19 | while(true){ 20 | if(node!=NULL){ 21 | s.push(node); 22 | node=node->left; 23 | } 24 | else{ 25 | if(s.empty()==true)break; 26 | node=s.top(); 27 | s.pop(); 28 | in.push_back(node->val); 29 | node=node->right; 30 | } 31 | 32 | } 33 | return in; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Day7/topviewofbt.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | //Function to return a list of nodes visible from the top view 5 | //from left to right in Binary Tree. 6 | vector topView(Node *root) 7 | { 8 | vector ans; 9 | if(root == NULL) return ans; 10 | map mpp; 11 | queue> q; 12 | q.push({root, 0}); 13 | while(!q.empty()) { 14 | auto it = q.front(); 15 | q.pop(); 16 | Node* node = it.first; 17 | int line = it.second; 18 | if(mpp.find(line) == mpp.end()) mpp[line] = node->data; 19 | 20 | if(node->left != NULL) { 21 | q.push({node->left, line-1}); 22 | } 23 | if(node->right != NULL) { 24 | q.push({node->right, line + 1}); 25 | } 26 | 27 | } 28 | 29 | for(auto it : mpp) { 30 | ans.push_back(it.second); 31 | } 32 | return ans; 33 | } 34 | 35 | }; 36 | -------------------------------------------------------------------------------- /Day7/verticalorder.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> verticalTraversal(TreeNode* root) { 4 | map>> nodes; 5 | queue>> todo; 6 | todo.push({root, {0, 0}}); 7 | while (!todo.empty()) { 8 | auto p = todo.front(); 9 | todo.pop(); 10 | TreeNode* node = p.first; 11 | int x = p.second.first, y = p.second.second; 12 | nodes[x][y].insert(node -> val); 13 | if (node -> left) { 14 | todo.push({node -> left, {x - 1, y + 1}}); 15 | } 16 | if (node -> right) { 17 | todo.push({node -> right, {x + 1, y + 1}}); 18 | } 19 | } 20 | vector> ans; 21 | for (auto p : nodes) { 22 | vector col; 23 | for (auto q : p.second) { 24 | col.insert(col.end(), q.second.begin(), q.second.end()); 25 | } 26 | ans.push_back(col); 27 | } 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Day10/roottonodepath.cpp: -------------------------------------------------------------------------------- 1 | bool getPath(TreeNode *root, vector &arr, int x) { 2 | // if root is NULL 3 | // there is no path 4 | if (!root) 5 | return false; 6 | 7 | // push the node's value in 'arr' 8 | arr.push_back(root->val); 9 | 10 | // if it is the required node 11 | // return true 12 | if (root->val == x) 13 | return true; 14 | 15 | // else check whether the required node lies 16 | // in the left subtree or right subtree of 17 | // the current node 18 | if (getPath(root->left, arr, x) || 19 | getPath(root->right, arr, x)) 20 | return true; 21 | 22 | // required node does not lie either in the 23 | // left or right subtree of the current node 24 | // Thus, remove current node's value from 25 | // 'arr'and then return false 26 | arr.pop_back(); 27 | return false; 28 | } 29 | vector Solution::solve(TreeNode* A, int B) { 30 | vector arr; 31 | if(A == NULL) { 32 | return arr; 33 | } 34 | getPath(A, arr, B); 35 | return arr; 36 | } 37 | -------------------------------------------------------------------------------- /Day6/zigzag.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> zigzagLevelOrder(TreeNode* root) { 4 | vector> result; 5 | if (root == NULL) { 6 | return result; 7 | } 8 | 9 | queue nodesQueue; 10 | nodesQueue.push(root); 11 | bool leftToRight = true; 12 | 13 | while ( !nodesQueue.empty()) { 14 | int size = nodesQueue.size(); 15 | vector row(size); 16 | for (int i = 0; i < size; i++) { 17 | TreeNode* node = nodesQueue.front(); 18 | nodesQueue.pop(); 19 | 20 | // find position to fill node's value 21 | int index = (leftToRight) ? i : (size - 1 - i); 22 | 23 | row[index] = node->val; 24 | if (node->left) { 25 | nodesQueue.push(node->left); 26 | } 27 | if (node->right) { 28 | nodesQueue.push(node->right); 29 | } 30 | } 31 | // after this level 32 | leftToRight = !leftToRight; 33 | result.push_back(row); 34 | } 35 | return result; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Day5/boundarytraversal.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | bool isLeaf(Node* root) { 3 | return !root->left && !root->right; 4 | } 5 | 6 | void addLeftBoundary(Node* root, vector &res) { 7 | Node* cur = root->left; 8 | while (cur) { 9 | if (!isLeaf(cur)) res.push_back(cur->data); 10 | if (cur->left) cur = cur->left; 11 | else cur = cur->right; 12 | } 13 | } 14 | void addRightBoundary(Node* root, vector &res) { 15 | Node* cur = root->right; 16 | vector tmp; 17 | while (cur) { 18 | if (!isLeaf(cur)) tmp.push_back(cur->data); 19 | if (cur->right) cur = cur->right; 20 | else cur = cur->left; 21 | } 22 | for (int i = tmp.size()-1; i >= 0; --i) { 23 | res.push_back(tmp[i]); 24 | } 25 | } 26 | 27 | void addLeaves(Node* root, vector& res) { 28 | if (isLeaf(root)) { 29 | res.push_back(root->data); 30 | return; 31 | } 32 | if (root->left) addLeaves(root->left, res); 33 | if (root->right) addLeaves(root->right, res); 34 | } 35 | public: 36 | vector printBoundary(Node *root) 37 | { 38 | vector res; 39 | if (!root) return res; 40 | 41 | if (!isLeaf(root)) res.push_back(root->data); 42 | 43 | addLeftBoundary(root, res); 44 | 45 | // add leaf nodes 46 | addLeaves(root, res); 47 | 48 | addRightBoundary(root, res); 49 | return res; 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Day3/prepostininonetraversal.cpp: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Definition for a binary tree node. 4 | * struct TreeNode { 5 | * int val; 6 | * TreeNode *left; 7 | * TreeNode *right; 8 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 10 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 11 | * }; 12 | */ 13 | class Solution { 14 | 15 | public: 16 | vector postorderTraversal(TreeNode* root) { 17 | stack> st; 18 | st.push({root, 1}); 19 | vector pre, in, post; 20 | if(root == NULL) return post; 21 | 22 | while(!st.empty()) { 23 | auto it = st.top(); 24 | st.pop(); 25 | 26 | // this is part of pre 27 | // increment 1 to 2 28 | // push the left side of the tree 29 | if(it.second == 1) { 30 | pre.push_back(it.first->val); 31 | it.second++; 32 | st.push(it); 33 | 34 | if(it.first->left != NULL) { 35 | st.push({it.first->left, 1}); 36 | } 37 | } 38 | 39 | // this is a part of in 40 | // increment 2 to 3 41 | // push right 42 | else if(it.second == 2) { 43 | in.push_back(it.first->val); 44 | it.second++; 45 | st.push(it); 46 | 47 | if(it.first->right != NULL) { 48 | st.push({it.first->right, 1}); 49 | } 50 | } 51 | // don't push it back again 52 | else { 53 | post.push_back(it.first->val); 54 | } 55 | } 56 | 57 | return post; 58 | } 59 | }; -------------------------------------------------------------------------------- /Day15/burntree.cpp: -------------------------------------------------------------------------------- 1 | int findMaxDistance(map*, BinaryTreeNode*> &mpp, BinaryTreeNode* target) { 2 | queue*> q; 3 | q.push(target); 4 | map*,int> vis; 5 | vis[target] = 1; 6 | int maxi = 0; 7 | while(!q.empty()) { 8 | int sz = q.size(); 9 | int fl = 0; 10 | for(int i = 0;ileft && !vis[node->left]) { 14 | fl = 1; 15 | vis[node->left] = 1; 16 | q.push(node->left); 17 | } 18 | if(node->right && !vis[node->right]) { 19 | fl = 1; 20 | vis[node->right] = 1; 21 | q.push(node->right); 22 | } 23 | 24 | if(mpp[node] && !vis[mpp[node]]) { 25 | fl = 1; 26 | vis[mpp[node]] = 1; 27 | q.push(mpp[node]); 28 | } 29 | } 30 | if(fl) maxi++; 31 | } 32 | return maxi; 33 | } 34 | BinaryTreeNode* bfsToMapParents(BinaryTreeNode* root, 35 | map*, BinaryTreeNode*> &mpp, int start) { 36 | queue*> q; 37 | q.push(root); 38 | BinaryTreeNode* res; 39 | while(!q.empty()) { 40 | BinaryTreeNode* node = q.front(); 41 | if(node->data == start) res = node; 42 | q.pop(); 43 | if(node->left) { 44 | mpp[node->left] = node; 45 | q.push(node->left); 46 | } 47 | if(node->right) { 48 | mpp[node->right] = node; 49 | q.push(node->right); 50 | } 51 | } 52 | return res; 53 | } 54 | int timeToBurnTree(BinaryTreeNode* root, int start) 55 | { 56 | map*, BinaryTreeNode*> mpp; 57 | BinaryTreeNode* target = bfsToMapParents(root, mpp, start); 58 | int maxi = findMaxDistance(mpp, target); 59 | return maxi; 60 | } 61 | -------------------------------------------------------------------------------- /Day13/nodesatk.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void markParents(TreeNode* root, unordered_map &parent_track, TreeNode* target) { 3 | queue queue; 4 | queue.push(root); 5 | while(!queue.empty()) { 6 | TreeNode* current = queue.front(); 7 | queue.pop(); 8 | if(current->left) { 9 | parent_track[current->left] = current; 10 | queue.push(current->left); 11 | } 12 | if(current->right) { 13 | parent_track[current->right] = current; 14 | queue.push(current->right); 15 | } 16 | } 17 | } 18 | public: 19 | vector distanceK(TreeNode* root, TreeNode* target, int k) { 20 | unordered_map parent_track; // node -> parent 21 | markParents(root, parent_track, target); 22 | 23 | unordered_map visited; 24 | queue queue; 25 | queue.push(target); 26 | visited[target] = true; 27 | int curr_level = 0; 28 | while(!queue.empty()) { /*Second BFS to go upto K level from target node and using our hashtable info*/ 29 | int size = queue.size(); 30 | if(curr_level++ == k) break; 31 | for(int i=0; ileft && !visited[current->left]) { 34 | queue.push(current->left); 35 | visited[current->left] = true; 36 | } 37 | if(current->right && !visited[current->right]) { 38 | queue.push(current->right); 39 | visited[current->right] = true; 40 | } 41 | if(parent_track[current] && !visited[parent_track[current]]) { 42 | queue.push(parent_track[current]); 43 | visited[parent_track[current]] = true; 44 | } 45 | } 46 | } 47 | vector result; 48 | while(!queue.empty()) { 49 | TreeNode* current = queue.front(); queue.pop(); 50 | result.push_back(current->val); 51 | } 52 | return result; 53 | } 54 | }; 55 | --------------------------------------------------------------------------------