From c1b1b05a6788a193a303f1de4484d769c3fd20e4 Mon Sep 17 00:00:00 2001 From: Rishi gupta <82485566+rishigupta877@users.noreply.github.com> Date: Sat, 15 Oct 2022 22:26:51 +0530 Subject: [PATCH] BurningTree here we use the MultiSource BFS to find the minimum time when a tree Burns. --- .../binary_tree/cpp/BurningTree.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 data_structures/binary_tree/cpp/BurningTree.cpp diff --git a/data_structures/binary_tree/cpp/BurningTree.cpp b/data_structures/binary_tree/cpp/BurningTree.cpp new file mode 100644 index 00000000..7410eef0 --- /dev/null +++ b/data_structures/binary_tree/cpp/BurningTree.cpp @@ -0,0 +1,61 @@ +/** + * 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> verticalOrder(TreeNode *root) + { + //Your code here + queue>>q; + vector>ans; + q.push({root,{0,0}}); + map>>m; + while(!q.empty()){ + + int n=q.size(); + + for(int i=0;ival}); + if(x.first->left!=NULL){ + + q.push({x.first->left,{x.second.first-1,x.second.second+1}}); + + + + } + if(x.first->right!=NULL){ + q.push({x.first->right,{x.second.first+1,x.second.second+1}}); + + } + + } + + } + for(auto i: m){ + vectorv; + for(auto j: i.second){ + + //cout<> verticalTraversal(TreeNode* root) { + return verticalOrder(root); + + } +};