zoukankan      html  css  js  c++  java
  • 38. Same Tree && Symmetric Tree

    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.

    思想: 无。能遍历即可。

    /**
     * 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 isSameTree(TreeNode *p, TreeNode *q) {
            if((p == NULL && q) || (p && q == NULL)) return false;
            if(p == NULL && q == NULL) return true;
            if(p->val != q->val) return false;
            return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
    };
    

    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.

    思想: 构造其镜像树。

    1. 递归。用 Same Tree方法判断即可

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    TreeNode* getMirror(TreeNode *root) {
        if(root == NULL) return NULL;
        TreeNode *p = new TreeNode(root->val);
        p->left = getMirror(root->right);
        p->right = getMirror(root->left);
        return p;
    }
    bool isSymmetricCore(TreeNode *root, TreeNode *root2) {
        if((!root && root2) || (root && !root2)) return false;
        if(!root && !root2) return true;
        if(root->val != root2->val) return false; 
        return isSymmetricCore(root->left, root2->left) && isSymmetricCore(root->right, root2->right);
    }
    class Solution {
    public:
        bool isSymmetric(TreeNode *root) {
            TreeNode *mirrorTree = getMirror(root);
            return isSymmetricCore(root, mirrorTree);
        }
    };
    

     2. 迭代。两棵树相同方法遍历即可。

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    TreeNode* getMirror(TreeNode *root) {
        if(root == NULL) return NULL;
        TreeNode *p = new TreeNode(root->val);
        p->left = getMirror(root->right);
        p->right = getMirror(root->left);
        return p;
    }
    bool pushChildNode(TreeNode *p1, TreeNode *p2, queue<TreeNode*> & qu, queue<TreeNode*>& qu2) {
        if(p1->left && p2->left) { qu.push(p1->left); qu2.push(p2->left); }
        else if(p1->left || p2->left) return false;
        if(p1->right && p2->right) { qu.push(p1->right); qu2.push(p2->right);}
        else if(p1->right || p2->right) return false;
        return true;
    }
    class Solution {
    public:
        bool isSymmetric(TreeNode *root) {
            if(root == NULL) return true;
            TreeNode *mirrorTree = getMirror(root);
            queue<TreeNode*> que1;
            queue<TreeNode*> que2;
            que1.push(root);
            que2.push(mirrorTree);
            while(!que1.empty() && !que2.empty()) {
                TreeNode *p1 = que1.front(), *p2 = que2.front();
                que1.pop(); que2.pop();
                if(p1->val != p2->val) return false;
                if(!pushChildNode(p1, p2, que1, que2)) return false;
            }
            if(!que1.empty() || !que2.empty()) return false;
            return true;
            
        }
    };
    
  • 相关阅读:
    Vue基本用法:vuex、axios 拦截器和vue-router路由导航守卫
    Vue基本用法:vue-router路由、refs属性和axios基本使用
    Tensorflow基本开发架构
    5. Vue3.x双向数据绑定
    4. Vue3.x中的事件方法详解
    3. Vue3.x中的事件方法入门
    2. Vue3绑定数据
    1. Vue3 入门 —— 简介、环境搭建
    2.5.1 MongoDB 介绍与基础
    2.6.8 Masstransit异常处理和总结
  • 原文地址:https://www.cnblogs.com/liyangguang1988/p/3940137.html
Copyright © 2011-2022 走看看