当前位置:编程学习 > C/C++ >>

leetcode_question_101 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.
 
Recursive:
 
[cpp]  
/** 
 * Definition for binary tree 
 * struct TreeNode { 
 *     int val; 
 *     TreeNode *left; 
 *     TreeNode *right; 
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
 * }; 
 */  
class Solution {  
public:  
    bool symmetric(TreeNode * left, TreeNode* right)  
    {  
        if(left== NULL && right == NULL)return true;  
        if(left && right && left->val == right->val)  
            return symmetric(left->right,right->left) && symmetric(left->left, right->right);  
        else return false;  
    }  
    bool isSymmetric(TreeNode *root) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        if(root==NULL)return true;  
        return symmetric(root->left,root->right);  
    }  
};  
 
中序遍历:
[cpp] 
/** 
 * Definition for binary tree 
 * struct TreeNode { 
 *     int val; 
 *     TreeNode *left; 
 *     TreeNode *right; 
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
 * }; 
 */  
class Solution {  
public:  
    void midvisit(TreeNode* root, vector<int>&path)  
    {  
        if(root)  
        {  
            midvisit(root->left,path);  
            path.push_back(root->val);  
            midvisit(root->right,path);  
        }  
    }  
    bool isSymmetric(TreeNode *root) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        if(root==NULL)return true;  
        vector<int> path;  
        midvisit(root,path);  
        int length = path.size();  
        bool flag = true;  
        for(int i = 0, j = length-1; i < j; ++i, --j)  
        {  
            if(path[i] != path[j]) {flag = false;break;}  
        }  
        return flag;  
    }  
};  
 
Iterative:
[cpp]  
/** 
 * Definition for binary tree 
 * struct TreeNode { 
 *     int val; 
 *     TreeNode *left; 
 *     TreeNode *right; 
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
 * }; 
 */  
class Solution {  
public:  
    bool isSymmetric(TreeNode *root) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        if(root==NULL)return true;  
        queue<TreeNode*> lt,rt;  
        if(root->left) lt.push(root->left);  
        if(root->right) rt.push(root->right);  
        TreeNode* l;  
        TreeNode* r;  
        while(!lt.empty() && !rt.empty())  
        {  
            l = lt.front();lt.pop();  
            r = rt.front();rt.pop();  
            if(l == NULL && r == NULL) continue;  
            if(l == NULL || r == NULL) return false;  
            if(l->val != r->val) return false;  
            lt.push(l->left);  
            lt.push(l->right);  
            rt.push(r->right);  
            rt.push(r->left);  
        }  
        if(lt.empty() && rt.empty())  
            return true;  
        else  
            return false;  
    }  
};  
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,