leetcode——101. 对称二叉树

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root:return True
        def Tree(p,q):
            if not p and not q:return True
            if p and q and p.val==q.val:
                return Tree(p.left,q.right) and Tree(p.right,q.left)
            return False
        return Tree(root.left,root.right)

执行用时 :24 ms, 在所有 python 提交中击败了76.38%的用户

内存消耗 :12 MB, 在所有 python 提交中击败了14.26%的用户

——2019.11.10

原文地址:https://www.cnblogs.com/taoyuxin/p/11831138.html

时间: 2024-08-30 15:46:15

leetcode——101. 对称二叉树的相关文章

Leetcode 101.对称二叉树

对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [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 说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分. 1 class Solution{ 2 public: 3 bool isSymmetric(TreeNode* root){ 4 if(root==NUL

LeetCode 101. 对称二叉树(Symmetric Tree)

题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [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 说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分. 解题思路 本题可用递归和迭代两种做法来求解. 递归做法是每次对于对称的两个节点,首先判断是否都为空,若都为空则返回true:否则判断两节点是否相等,若相等则返回它们对应的子节

LeetCode 101.对称二叉树 - JavaScript

题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / 2 2 / \ / 3 4 4 3 解法 1:递归检查 根据题目"对称"的定义,递归过程如下: 对称节点的 val 是否相同 依次递归对称节点的 left1 和 right2.right1 和 left2(结合上面的例子更好理解) 代码实现如下: // ac地址:https://leetcode-cn.com/problems/symmetric-tree/ // 原文地

101. 对称二叉树,c++迭代递归解法

101. 对称二叉树,c++迭代递归解法 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树?[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 这道题可以用迭代和递归两种方法求解 迭代法代码如下,主要思想是,将树的左右分支放入两个队列中.因为题目是判断两个数是否对称,所以在将节点a的孩子放左队列时,先放a的右子结点,再放a的左子结点:在将节点b的孩子

2.(101)对称二叉树

2.(101)对称二叉树 2020年3月20日 Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following [1,2,2,null,3,nu

【LeetCode】101. 对称二叉树

题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [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 说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分. 本题同[剑指Offer]面试题28. 对称的二叉树 思路一:递归 利用镜像. 代码 时间复杂度:O(n) 空间复杂度:O(n) class Solution { public:

101. 对称二叉树

给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [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说明: 如果你可以运用递归和迭代两种方法解决这个问题,会很加分 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/symmetric-tree 1 public class Symmetr

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

剑指offer (19) 二叉树镜像 对称二叉树

题目: 输入一个二叉树,输出其镜像. BinTreeNode* ReverseTree(BinTreeNode* pRoot) { if (pRoot == NULL) return NULL; BinTreeNode* pLeftReverse = ReverseTree(pRoot->left); BinTreeNode* pRightReverse = ReverseTree(pRoot->right); pRoot->left = pRightReverse; pRoot->