leetcode(101):Symmetric Tree

news/2024/7/7 5:30:49 标签: leetcode

题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
这里写图片描述

对于题目的分析:这个题目要求判断给定的这棵树是不是关于根节点对称。对于根节点而言,首先需要判断根节点是不是为None形式,如果为None形式,则肯定是对称的,然后判断根节点的二个子树的节点情况,使左右子树关于根节点对称,则再判断在原来判断二颗树是否相同的基础上加以修改,也就是左子树的左节点与右子树的右节点,左子树的右节点与右子树的左节点来进行判断。
python代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root == None:
            return True
        l = root.left
        r = root.right
        return self.isSame(l, r)

    def isSame(self, l, r):
        if l and r:
            return l.val == r.val and self.isSame(l.left, r.right) and self.isSame(l.right, r.left)
        return l is r

大佬的代码如下:
recursively递归的解法,因为树的结构是递归的,所以用递归的方法来求解会比较容易书写。

class Solution:
  def isSymmetric(self, root):
    if root is None:
      return True
    else:
      return self.isMirror(root.left, root.right)

  def isMirror(self, left, right):
    if left is None and right is None:
      return True
    if left is None or right is None:
      return False
    if left.val == right.val:
      outPair = self.isMirror(left.left, right.right)
      inPiar = self.isMirror(left.right, right.left)
      return outPair and inPiar
    else:
      return False

好吧,我跟大佬的思想是一致的,还是很开心的。递归是用栈来实现的,迭代大佬们也是用栈来实现的。

迭代的解法:

 class Solution2:
  def isSymmetric(self, root):
    if root is None:
      return True

    stack = [[root.left, root.right]]

    while len(stack) > 0:
      pair = stack.pop(0)
      left = pair[0]
      right = pair[1]

      if left is None and right is None:
        continue
      if left is None or right is None:
        return False
      if left.val == right.val:
        stack.insert(0, [left.left, right.right])

        stack.insert(0, [left.right, right.left])
      else:
        return False
    return True

对于递归而言,也是利用了栈的结构,其实跟递归的求解是一致的,只是说不同的表现手法,也就是利用了一对一对的入栈出栈来进行处理。

此题得到的思考:
1,在做题的时候,要思考模式,自己之前思考过什么模式,如何应用。
2,能简化书写的要简化书写,但逻辑不能简化。
3,要注意,对于树而言,比较顺畅的是递归的做法,但如果递归的做法做不出来的时候,一定要换一条路子走啊,采用迭代的方法,利用list来进行处理,完成操作。。当然也有stack,queue等,但其实说实话,它们也都是披着老虎面具的羊羔子,实质上还是list。处理问题要灵活多变。


http://www.niftyadmin.cn/n/1798846.html

相关文章

消除云界限——NetApp更新高端存储

云本身就是无形的、灵动的。但是借用了“云”概念的计算却非要分清到底是公有、私有还是混合。 如今,人们又在想方设法消除公有云、私有云和混合云之间的界限,让数据和应用能够在“自由云域”中畅行。无论是公有云、私有云还是混合云,它们对于…

linux上部署nodejs

linux上部署nodejs 环境:# lsb_release -aLSB Version: :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarchDistributor ID: CentOSDescript…

leetcode(617):Merge Two Binary Trees

题目要求: Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two no…

POJ 1247 数组首尾求和判定相等 枚举法

此题把题意弄懂后就很容易&#xff0c;S顺时针走&#xff0c;E逆时针走&#xff0c;确定一个分割点该处两人相遇时途经的客人数之和相等&#xff0c;直接枚举。 #include <iostream> using namespace std; //s顺时针走&#xff0c;e逆时针走&#xff0c;问他们在哪里相遇…

开源 免费 java CMS - FreeCMS1.8 互动信件

2019独角兽企业重金招聘Python工程师标准>>> 项目地址&#xff1a;http://code.google.com/p/freecms/ 互动信件 1. 部门信件管理 部门信件指接收人为部门的信件,从左侧管理菜单点击部门信件进入。 admin可以管理所有部门信件&#xff0c;其他用户可以管理自己…

leetcode(669):Trim a Binary Search Tree

题目&#xff1a;Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R > L). You might need to change the root of the tree, so the result should return the new root of the tr…

Linux-nat与nat

一、Linux-nat1、SANT应用环境&#xff1a;2、SNAT原理图3、SNAT实现步骤4、网关使用动态公网IP5、DNAT应用环境&#xff1a;6、DNAT原理7、DNAT使用步骤8、使用DNAT修改端口号二、真实路由器-nat1、分类&#xff1a;a、静态NAT(一个内部地址对应一个唯一的外部地址&#xff0c…

C++ STL string类相关

之所以抛弃char*的字符串而选用C标准程序库中的string类&#xff0c;是因为他和前者比较起来&#xff0c;不必 担心内存是否足够、字符串长度等等&#xff0c;而且作为一个类出现&#xff0c;他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 进行赋值…