leetcode 13: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.


[cpp]  view plain copy
  1. /** 
  2.  * Definition for binary tree 
  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.   
  13.     bool isSymRec(TreeNode *left, TreeNode * right){  
  14.         if(left == NULL && right==NULL){  
  15.             return true;  
  16.         } else if(left == NULL || right == NULL) {  
  17.             return false;  
  18.         }  
  19.           
  20.         return ( left->val == right->val && isSymRec( left->left, right->right) &&   
  21.             isSymRec( left->right, right->left) );  
  22.         //both are not NULL.         
  23.     }  
  24.   
  25.     bool isSymmetric(TreeNode *root) {  
  26.         // Start typing your C/C++ solution below  
  27.         // DO NOT write int main() function  
  28.         if(root == NULL) return true;  
  29.         return isSymRec( root->left, root->right);  
  30.     }  
  31. };  


[java]  view plain copy
  1. /** 
  2.  * Definition for binary tree 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public boolean isSymmetric(TreeNode root) {  
  12.         // Start typing your Java solution below  
  13.         // DO NOT write main() function  
  14.         if(root==null) {  
  15.             return true;  
  16.         }  
  17.           
  18.         return isSysRec(root.left, root.right);  
  19.     }  
  20.       
  21.     private boolean isSysRec(TreeNode left, TreeNode right){  
  22.         if(left==null && right==null) {  
  23.             return true;  
  24.         } else if( left==null || right==null) {  
  25.             return false;  
  26.         }  
  27.           
  28.         return (left.val==right.val) && isSysRec(left.left, right.right) && isSysRec(left.right, right.left);  
  29.     }  
  30. }  


iterative approach:

[cpp]  view plain copy
  1. /** 
  2.  * Definition for binary tree 
  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 isSymmetric(TreeNode *root) {  
  13.         // Start typing your C/C++ solution below  
  14.         // DO NOT write int main() function  
  15.         if(root==nullptr) return true;  
  16.         queue<TreeNode*> q1;  
  17.         queue<TreeNode*> q2;  
  18.           
  19.         q1.push(root->left);  
  20.         q2.push(root->right);  
  21.           
  22.         while( !q1.empty() || !q2.empty()) {  
  23.             TreeNode* t1 = q1.front();  
  24.             TreeNode* t2 = q2.front();  
  25.             q1.pop();  
  26.             q2.pop();  
  27.               
  28.             if(t1==nullptr && t2==nullptr) continue;  
  29.             if(t1==nullptr || t2==nullptr) return false;  
  30.               
  31.             if(t1->val!=t2->val) return false;  
  32.               
  33.             q1.push(t1->left);  
  34.             q1.push(t1->right);  
  35.             q2.push(t2->right);  
  36.             q2.push(t2->left);  
  37.         }  
  38.           
  39.         return true;  
  40.           
  41.     }    
  42. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值