leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0872.cpp (643B)
0 class Solution {
1 vector<int> sequence(TreeNode *root) {
2 if (!root) return {};
4 vector<int> res;
5 stack<TreeNode *> st;
7 st.push(root);
8 while (!st.empty()) {
9 TreeNode *root = st.top();
10 st.pop();
11 if (!root->left && !root->right) {
12 res.push_back(root->val);
13 continue;
14 }
15 if (root->left) st.push(root->left);
16 if (root->right) st.push(root->right);
17 }
19 return res;
20 }
22 public:
23 bool leafSimilar(TreeNode *root1, TreeNode *root2) { return sequence(root1) == sequence(root2); }
24 };