zoukankan      html  css  js  c++  java
  • 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
     
    还是运用递归的思想,挺简单好用的。
    public boolean isSymmetric(TreeNode root) {
            if(root == null){
                return true;
            }
            return isSymmetric(root.left,root.right);
        }
    
        private boolean isSymmetric(TreeNode left, TreeNode right) {
            if(left == null && right == null){
                return true;
            }else if(left == null || right == null){
                return false;
            }else if(left.val != right.val){
                return false;
            }
            return isSymmetric(left.left,right.right) && isSymmetric(left.right,right.left);
        }

    ——2020.7.1

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    要离职了。
    上海找工作经历
    1.6. 三基色LED
    1.5. 板载LED PWM模式
    1.4. 板载LED控制
    1.3. 硬件篇之IO口(视频连接)
    1.2 Hello World
    1.8. 数码管
    ESP32编译自己的micropython固件
    1.1 准备工作
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11831138.html
Copyright © 2011-2022 走看看