【题目】

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,null,3] is not:

    1
/ \
2 2
\ \
3 3

【思路】

类似于100判断树是否相同,根节点开始分左右两路比较,三种情况讨论。p和q=null、p或q=null、p和q的val相同迭代。

不同在于mirror正好相反, 对left和right比较,即是fun(p1.left,p2.right)&&fun(p1.right,p2.left)。

【代码】

class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return fun(root.left,root.right);
} public boolean fun(TreeNode p1,TreeNode p2) {
if(p1==null&&p2==null)
return true;
if(p1==null||p2==null)
return false;
if(p1.val==p2.val)
return fun(p1.left,p2.right)&&fun(p1.right,p2.left);
return false;
}
}

[Leetcode 101]判断对称树 Symmetric Tree的更多相关文章

  1. LeetCode 101. Symmetric Tree 判断对称树 C++

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. [LeetCode] Symmetric Tree 判断对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. leetcode-101. 判断对称树 · Tree + 递归

    题面 判断给定二叉树是否对称. Note : empty tree is valid. 算法 1. 根节点判空,若空,则返回true;(空树对称) 2. 根节点不空,递归判断左右子树.如果左右孩子都空 ...

  4. [Swift]LeetCode101. 对称二叉树 | Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. 【Leetcode】【Easy】Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. LeetCode 100. 相同的树(Same Tree) 2

    100. 相同的树 100. Same Tree 题目描述 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 每日一算法2019/5 ...

  7. [Leetcode 100]判断二叉树相同 Same Tree

    [题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...

  8. LeetCode Same Tree (判断相同树)

    题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...

  9. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

随机推荐

  1. English trip V1 - 11.What's That? 那是什么?Teacher:Patrick Key:There's/There are

    In this lesson you will learn to describe the position of an object. 在本课中,您将学习如何描述对象的位置. 课上内容(Lesson ...

  2. C#获取网页的HTML码、下载网站图片

    1.根据URL请求获取页面HTML代码 /// <summary> /// 获取网页的HTML码 /// </summary> /// <param name=" ...

  3. linux网络配置命令(一)——ifconfig

    linux网络配置命令(一)——ifconfig ifconfig 查看.配置网卡信息.已过时,推荐使用ip命令 格式:  ifconfig [interface]                   ...

  4. 【PowerDesigner】【6】Table视图同时显示Code和Name

    效果图: —————————————————————— 步骤: 文字版: 1,顶部工具栏Tools→Display Preference 2,Columns→List columns右侧按钮 3,勾选 ...

  5. 26. Remove Duplicates from Sorted Array C++ 删除排序数组中的重复项

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 双指针,注意初始时左右指针指向首元素! class Solutio ...

  6. 【转】大型Vuex项目 ,使用module后, 如何调用其他模块的 属性值和方法

    Vuex 允许我们把 store 分 module(模块).每一个模块包含各自的状态.mutation.action 和 getter. 那么问题来了, 模块化+命名空间之后, 数据都是相对独立的, ...

  7. git reset --hard 恢复

    git reset --hard ,再然后,悲剧上演~ 恢复方法: 使用 git reflog 来找到最近提交的信息,这里贴出部分信息: F:\voidy>git reflog WARNING: ...

  8. Qt中漂亮的几款QSS

    /* === Shared === */QStackedWidget, QLabel, QPushButton, QRadioButton, QCheckBox, QGroupBox, QStatus ...

  9. Eclipse导出WAR包

    参考: https://jingyan.baidu.com/article/ab0b56309110b4c15afa7de2.html

  10. 把旧系统迁移到.Net Core 2.0 日记(8) - EASYUI datagrid+ Dapper+ 导出Excel

    迁移也没太大变化,有一个, 之前的Request.QueryString 是返回NameValueCollection, 现在则是返回整个字符串. 你要改成Request.Query[“key”] 直 ...