zoukankan      html  css  js  c++  java
  • [LeetCode] 101. 对称二叉树

    题目链接 : https://leetcode-cn.com/problems/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
    

    思路:

    这道题和 上一题 100. 相同的树是一样的

    我们只要比较root左右树是否对称(和是否相同的)就行了

    思路一:递归

    思路二:迭代

    代码:

    思路一:

    # 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: TreeNode) -> 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)
    

    java

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root == null) return true;
            return Tree(root.left, root.right);
    
        }
    
        public boolean Tree(TreeNode p, TreeNode q) {
            if (p == null && q == null) return true;
            if (p != null && q != null && p.val == q.val) return Tree(p.left, q.right) && Tree(p.right, q.left);
            else return false;
        }
    }
    

    思路二:

    # 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: TreeNode) -> bool:
            if not root: return True
            def Tree(p, q):
                stack = [(q, p)]
                while stack:
                    a, b = stack.pop()
                    if not a and not b:
                        continue
                    if a and b and a.val == b.val:
                        stack.append((a.left, b.right))
                        stack.append((a.right,b.left))
                    else:
                        return False
                return True
            return Tree(root.left, root.right)
    

    java

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root == null) return true;
            return Tree(root.left, root.right);
    
        }
         public boolean Tree(TreeNode p, TreeNode q) {
            Deque<TreeNode> stack1 = new LinkedList<>();
            Deque<TreeNode> stack2 = new LinkedList<>();
            stack1.push(p);
            stack2.push(q);
            while (!stack1.isEmpty() && !stack2.isEmpty()) {
                TreeNode a = stack1.pop();
                TreeNode b = stack2.pop();
                if (a == null && b == null) continue;
                if (a != null && b != null && a.val == b.val) {
                    stack1.push(a.left);
                    stack1.push(a.right);
                    stack2.push(b.right);
                    stack2.push(b.left);
                } else return false;
            }
            return stack1.isEmpty() && stack2.isEmpty();
        }
    }
    
  • 相关阅读:
    三元表达式 列表和字典推导式 函数对象 名称空间 作用域 global和nonlocal 函数装饰器 枚举对象
    函数参数 打散机制 字符串比较 返回值
    函数简介
    三种字符串的介绍 文件的读写
    字符编码
    数据类型及其常用方法 数据类型转换 可变与不可变 值拷贝与深浅拷贝
    流程控制 while和for循环
    变量命名规范 常量 输入和输出 注释 数据类型 运算符 逻辑运算符
    语言分类 编译型和解释型语言分析 环境变量 代码执行的方式 pip介绍 变量
    Python django tests
  • 原文地址:https://www.cnblogs.com/powercai/p/11085309.html
Copyright © 2011-2022 走看看