Symmetric Tree
题目要求:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / 2 2 / \ / 3 4 4 3
But the following is not:
1 / 2 2 \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
OJ‘s Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where ‘#‘ signifies a path terminator where no node exists below.
Here‘s an example:
1 / 2 3 / 4 5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}"
.
本题暂用递归解法,具体程序(4ms)如下:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void updateLVec(TreeNode *left, vector<int>& lVec) 13 { 14 if(left) 15 { 16 lVec.push_back(left->val); 17 if(left->left || left->right) 18 { 19 if(left->left) 20 updateLVec(left->left, lVec); 21 else 22 lVec.push_back(INT_MIN); 23 if(left->right) 24 updateLVec(left->right, lVec); 25 else 26 lVec.push_back(INT_MIN); 27 } 28 } 29 } 30 31 void updateRVec(TreeNode *right, vector<int>& rVec) 32 { 33 if(right) 34 { 35 rVec.push_back(right->val); 36 if(right->left || right->right) 37 { 38 if(right->right) 39 updateRVec(right->right, rVec); 40 else 41 rVec.push_back(INT_MIN); 42 if(right->left) 43 updateRVec(right->left, rVec); 44 else 45 rVec.push_back(INT_MIN); 46 } 47 } 48 } 49 50 bool isSymmetric(TreeNode* root) { 51 if(!root || (!root->left && !root->right)) 52 return true; 53 54 if((!root->left && root->right) || (root->left && !root->right)) 55 return false; 56 57 vector<int> lVec, rVec; 58 updateLVec(root->left, lVec); 59 updateRVec(root->right, rVec); 60 61 int szLVec = lVec.size(); 62 int szRVec = rVec.size(); 63 if(szLVec != szRVec) 64 return false; 65 66 for(int i = 0; i < szLVec; i++) 67 { 68 if(lVec[i] != rVec[i]) 69 return false; 70 } 71 72 return true; 73 } 74 };
虽然最终解决了问题,但代码还是复杂了。别人的代码(4ms)真叫一个简单呐:
1 bool isSymmetric(TreeNode *root) 2 { 3 if (!root) return true; 4 return isSymmetric(root->left, root->right); 5 } 6 bool isSymmetric(TreeNode *lt, TreeNode *rt) 7 { 8 if (!lt && !rt) return true; 9 if (lt && !rt || !lt && rt || lt->val != rt->val) return false; 10 return isSymmetric(lt->left, rt->right) && isSymmetric(lt->right, rt->left); 11 }
Same Tree
题目要求:
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
有了上一道题的基础,这道题就很简单了,具体程序(0ms)如下:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSameTree(TreeNode* p, TreeNode* q) { 13 if(!p && !q) 14 return true; 15 return isSameTreeSub(p, q); 16 } 17 18 bool isSameTreeSub(TreeNode *lt, TreeNode *rt) 19 { 20 if(!lt && !rt) 21 return true; 22 if((!lt && rt) || (lt && !rt) || (lt->val != rt->val)) 23 return false; 24 return isSameTree(lt->left, rt->left) && isSameTree(lt->right, rt->right); 25 } 26 };