leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1325.cpp (996B)


0 class Solution { 1 public: 2 TreeNode *removeLeafNodes(TreeNode *root, int target) { 3 TreeNode dummy(-1, root, nullptr); 4 unordered_map<TreeNode *, TreeNode *> um = {{root, &dummy}}; 5 stack<TreeNode *> st({root}); 6 while (!st.empty()) { 7 TreeNode *root = st.top(); 8 if (root->val < 0) { 9 st.pop(); 10 root->val = -root->val; 11 if (!root->left && !root->right && root->val == target) { 12 TreeNode *parent = um[root]; 13 (parent->left == root ? parent->left : parent->right) = nullptr; 14 } 15 continue; 16 } 17 root->val = -root->val; 18 if (root->left) { 19 um[root->left] = root; 20 st.push(root->left); 21 } 22 if (root->right) { 23 um[root->right] = root; 24 st.push(root->right); 25 } 26 } 27 return dummy.left; 28 } 29 };