zoukankan      html  css  js  c++  java
  • Leecode刷题之旅-C语言/python-101对称二叉树

    /*
     * @lc app=leetcode.cn id=101 lang=c
     *
     * [101] 对称二叉树
     *
     * https://leetcode-cn.com/problems/symmetric-tree/description/
     *
     * algorithms
     * Easy (45.30%)
     * Total Accepted:    23.8K
     * Total Submissions: 52.4K
     * Testcase Example:  '[1,2,2,3,4,4,3]'
     *
     * 给定一个二叉树,检查它是否是镜像对称的。
     * 
     * 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
     *  
     * 
     * ⁠   1
     * ⁠  / 
     * ⁠ 2   2
     * ⁠/  / 
     * 3  4 4  3
     * 
     * 
     * 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
     * 
     * ⁠   1
     * ⁠  / 
     * ⁠ 2   2
     * ⁠     
     * ⁠  3    3
     * 
     * 
     * 说明:
     * 
     * 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
     * 
     */
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    bool com(struct TreeNode* a,struct TreeNode* b);
    
    bool isSymmetric(struct TreeNode* root) {
    
        if(root == NULL)
    
            return true;
    
        return com(root->left,root->right);
    }
    bool com(struct TreeNode* a,struct TreeNode* b)
    {
        if(a == NULL&&b == NULL)
            return true;
        else
        {
            if(a == NULL||b == NULL)
                return false;
            else if(a -> val==b -> val)
                return com(a->left,b->right)&&com(a->right,b->left);
            else
                return false;
        }      
    }

    这里应用递归算法。需要引用一个自己创建的函数(只有一个root是无法递归出来的)

    其实有点像前一道相同的树那道题,这里判断的对称的本质就是 左子树的右子树等于右子树的左子树 就是对称。

    ----------------------------------------------------------------------------------------------------------------------------------------------------

    python:

    #
    # @lc app=leetcode.cn id=101 lang=python3
    #
    # [101] 对称二叉树
    #
    # https://leetcode-cn.com/problems/symmetric-tree/description/
    #
    # algorithms
    # Easy (45.30%)
    # Total Accepted:    23.8K
    # Total Submissions: 52.4K
    # Testcase Example:  '[1,2,2,3,4,4,3]'
    #
    # 给定一个二叉树,检查它是否是镜像对称的。
    # 
    # 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
    # 
    # ⁠   1
    # ⁠  / 
    # ⁠ 2   2
    # ⁠/  / 
    # 3  4 4  3
    # 
    # 
    # 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
    # 
    # ⁠   1
    # ⁠  / 
    # ⁠ 2   2
    #
    # ⁠  3    3
    # 
    # 
    # 说明:
    # 
    # 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
    # 
    #
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Solution:
        def isSymmetric(self, root):
            if root == None:
                return True
            else:
                return self.isSym(root.left, root.right)
            
        def isSym(self, p, q):
            if p == None and q == None:
                return True
            elif p == None or q == None:
                return False
            elif p.val == q.val:
                return self.isSym(p.left, q.right) and self.isSym(p.right, q.left)
            else:
                return False  
  • 相关阅读:
    一种解决h5页面背景音乐不能自动播放的方案
    VUE中的v-if与v-show
    setInterval(code, time)中code传递参数办法
    CSS——图片替换方法比较
    JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson
    JSON(二)——JavaScript中js对象与JSON格式字符串的相互转换
    JSON(一)——JSON与JavaScript的关系
    详解Ajax请求(四)——多个异步请求的执行顺序
    详解Ajax请求(三)——jQuery对Ajax的实现及serialize()函数对于表单域控件参数提交的使用技巧
    详解Ajax请求(二)——异步请求原理的分析
  • 原文地址:https://www.cnblogs.com/lixiaoyao123/p/10523377.html
Copyright © 2011-2022 走看看