leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0101.cpp (797B)
0 class Solution { 1 string preorder(TreeNode *root, bool left) { 2 if (!root) return ""; 3 4 string res; 5 stack<TreeNode *> st; 6 st.push(root); 7 while (!st.empty()) { 8 TreeNode *root = st.top(); 9 st.pop(); 10 if (!root) { 11 res += "-"; 12 continue; 13 } 14 res += root->val; 15 if (left) { 16 st.push(root->right); 17 st.push(root->left); 18 } else { 19 st.push(root->left); 20 st.push(root->right); 21 } 22 } 23 return res; 24 } 25 26 public: 27 bool isSymmetric(TreeNode *root) { 28 if (!root) return false; 29 30 return preorder(root->left, true) == preorder(root->right, false); 31 } 32 };