Symmetric Tree(对称二叉树)
来源:https://leetcode.com/problems/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,null,3]
is not:
1
/ \
2 2
\ \
3 3
递归:
1. 若根节点为null,返回true
2. 若根节点不为null,对其左右子树进行递归地比较:若两个节点都为null,返回true;若一个为null,另一个不为null,返回false;若两个节点(A,B)值相等,则返回 (A.left, B.right) 的比较结果 && (A.right, B.left) 的比较结果。
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 compareLR(TreeNode left, TreeNode right) {
if(left == null && right == null) {
return true;
}
if((left == null && right != null) || (left != null && right == null)) {
return false;
}
if(left.val == right.val) {
return compareLR(left.right, right.left) && compareLR(left.left, right.right);
}
return false;
}
public boolean isSymmetric(TreeNode root) {
if(root == null) {
return true;
}
return compareLR(root.left, root.right);
}
}
Python
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetrical(self, pRoot):
def comRoot(left, right):
if not left:
return not right
if not right or (left.val != right.val):
return False
return comRoot(left.right, right.left) and comRoot(left.left, right.right)
if not pRoot:
return True
return comRoot(pRoot.left, pRoot.right)
迭代:
1. 若根节点为null,返回true
2. 若根节点不为null,将其左右两个子节点入队列
3. 从队列中取出两个节点,若两个节点都为null,返回true;若一个为null,另一个不为null,返回false;若两个节点(A,B)值相等,将他们的两个子节点按照A.left,B.right,A.right,B.left的顺序入队列,重复执行该步,直到队列中不再有节点。
Symmetric Tree(对称二叉树)的更多相关文章
- [Leetcode] Symmetric tree 对称二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 第28题:leetcode101:Symmetric Tree对称的二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Symmetric Tree 对称树
判断一棵二叉树是否为对称的树.如 1 / \ 2 2 / \ / \ 3 4 4 3 观察上面的树可以看出:左子树的右子树等于右子树的左子树,左子树的左子树等于右子树的右子树. 首先可以使用递归.递归 ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...
随机推荐
- 003-awk 命令使用
awk 命令使用 截取符合条件的列 awk 先读取第一行后,再去处理数据 例子: cut 不能截取分隔符为空格,tab之类的文件或字符串,但awk可以 [root@zabbix lianxi]# df ...
- First-hitting-time model
见wiki: https://en.wikipedia.org/wiki/First-hitting-time_model
- python-类对象的比较
#类对象的比较 class Person: def __init__(self,age,height): self.age=age self.height=height def __eq__(self ...
- 【leetcode】1170. Compare Strings by Frequency of the Smallest Character
题目如下: Let's define a function f(s) over a non-empty string s, which calculates the frequency of the ...
- 理解厂商前缀 -webkit- / -moz- / -ms- / -o-
CSS3规范如果想要达到W3C的推荐标准状态还需要不断改进.浏览器则通常在W3C开发标准的过程中就会体现这些特性.这样,标准在最终敲定之前就能知道哪些地方还能进一步改进. 在包含某个特性的的初始阶段, ...
- React native 平时积累笔记
常用插件: react-native-check-box 复选框react-native-sortable-listview 列表拖拽排序 react-native-doc-viewer 预览组件 r ...
- 跳转控制语句return
return语句的作用不是为了跳出循环,更常用的功能是结束一个方法,也就是退出一个方法,跳转到上层调用的方法处. 演示案例: 结束循环其实是结束了main方法 public static void m ...
- php调接口批量同步本地文件到cos或者oss
代码: <?php namespace Main\Controller; use Common\Library\Vendor\ElasticSearch; use Common\Library\ ...
- javascript中面向对象的两种构建方式(构造函数)和(原型模式的区别)
1.构造函数模式--->alert的结果为false <!DOCTYPE html> <html lang="en"> <head> &l ...
- 笨办法学Python(learn python the hard way)--练习程序42
下面是练习42,基于python3 #ex42.py 1 class TheThing(object): 2 #__init__为class设置内部变量的方式,正常情况下函数内的变量与外部没有关联,但 ...