leetcode

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

0814.cpp (930B)


0 class Solution { 1 public: 2 TreeNode *pruneTree(TreeNode *root) { 3 TreeNode dummy(1, root, nullptr); 4 unordered_set<TreeNode *> has; 5 stack<TreeNode *> st; 6 st.push(&dummy); 7 while (!st.empty()) { 8 TreeNode *root = st.top(); 9 if (!root) { 10 st.pop(); 11 root = st.top(), st.pop(); 12 if (has.count(root->left)) 13 has.insert(root); 14 else 15 root->left = nullptr; 16 17 if (has.count(root->right)) 18 has.insert(root); 19 else 20 root->right = nullptr; 21 22 if (root->val == 1) has.insert(root); 23 continue; 24 } 25 st.push(nullptr); 26 if (root->left) st.push(root->left); 27 if (root->right) st.push(root->right); 28 } 29 return dummy.left; 30 } 31 };