leetcode

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

0872.cpp (643B)


      1 class Solution {
      2     vector<int> sequence(TreeNode *root) {
      3         if (!root) return {};
      4 
      5         vector<int> res;
      6         stack<TreeNode *> st;
      7 
      8         st.push(root);
      9         while (!st.empty()) {
     10             TreeNode *root = st.top();
     11             st.pop();
     12             if (!root->left && !root->right) {
     13                 res.push_back(root->val);
     14                 continue;
     15             }
     16             if (root->left) st.push(root->left);
     17             if (root->right) st.push(root->right);
     18         }
     19 
     20         return res;
     21     }
     22 
     23   public:
     24     bool leafSimilar(TreeNode *root1, TreeNode *root2) { return sequence(root1) == sequence(root2); }
     25 };