zoukankan      html  css  js  c++  java
  • leetcode-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.

    原本我的做法,是先求出一棵树的镜像,然后再查看这样的两棵树是不是相等的(isSameTree()请查看本博客leetcode-same tree)

    但是卡在了{1,2}这个测试例子上,怎么都通不过。。我的代码如下:(失败)

    /**
     * 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
            TreeNode *root1 = root;
            mirror(root1);
            return isSameTree(root,root1);
            
        }
        
        void mirror(TreeNode *root){
            if(root == NULL){
                return;
            }
            if(root->left == NULL && root->right == NULL){
                return;
            }
            //exchange
            TreeNode *temp = root->left;
            root->left = root->right;
            root->right = temp;
            
            if(root->left){
                mirror(root->left);
            }
            if(root->right){
                mirror(root->right);
            }
        }
        
        bool isSameTree(TreeNode *p, TreeNode *q) {
            if(p==NULL && q==NULL){
                return true;
            }
            else if(p==NULL || q==NULL){
                return false;
            }
            
            if(p->val == q->val){
                return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
            }
            else{
                return false;
            }
        }
    };

    后来查看解答,发现就是node->left->val == node->right->val;node->right->val == node->left->val的一个递归:所以就有如下代码,省去了求镜像的过程:

    /**
     * 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
            return isSame(root,root);
            
        }
        
        bool isSame(TreeNode *p, TreeNode *q) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(p==NULL && q==NULL){
                return true;
            }
            else if(p==NULL || q==NULL){
                return false;
            }
            
            if(p->val == q->val){
                return isSame(p->left,q->right) && isSame(p->right,q->left);
            }
            else{
                return false;
            }
        }
    };
  • 相关阅读:
    正则表达式基础知识
    成功的基本法则
    Java实现简单的格式化信函生成器
    C实现哈希表
    C实现求解给定文本中以指定字符开头和结尾的子串数量的三种算法
    Java实现求解二项式系数及代码重构
    Java 异常处理学习总结
    C实现大整数幂求模问题的两种算法
    linux 学习前言
    提高编程能力的10种方法
  • 原文地址:https://www.cnblogs.com/viltran/p/3249014.html
Copyright © 2011-2022 走看看