问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4068 访问。

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [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

说明:如果你可以运用递归和迭代两种方法解决这个问题,会很加分。


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

Note:Bonus points if you could solve it both recursively and iteratively.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4068 访问。

public class Program {

    public static void Main(string[] args) {
var root = new TreeNode(1) {
left = new TreeNode(2),
right = new TreeNode(2)
}; var res = IsSymmetric(root);
Console.WriteLine(res); Console.ReadKey();
} public static bool IsSymmetric(TreeNode root) {
return Symmetric(root, root);
} public static bool Symmetric(TreeNode node1, TreeNode node2) {
//都为空时,递归终止,为镜像二叉树
if(node1 == null && node2 == null) return true;
//有一个为空时,递归终止,不是镜像二叉树
if(node1 == null || node2 == null) return false;
//递归判定
return node1.val == node2.val &&
Symmetric(node1.left, node2.right) &&
Symmetric(node1.right, node2.left);
} public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4068 访问。

True

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#101-对称二叉树(Symmetric Tree)的更多相关文章

  1. C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4074 访问. 给定一个二叉树,判断它是否是高度平衡的二叉树. 本 ...

  2. LeetCode刷题笔记-递归-反转二叉树

    题目描述: 翻转一棵二叉树. 解题思路: 1.对于二叉树,立马递归 2.先处理 根节点,不需改动 3.处根的左子树和右子树需要交换位置 4.递归处理左子树和右子树.步骤见1-3步 Java代码实现: ...

  3. C#LeetCode刷题之#226-翻转二叉树(Invert Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4080 访问. 翻转一棵二叉树. 输入: 4    /   \   ...

  4. C#LeetCode刷题之#617-合并二叉树​​​​​​​​​​​​​​(Merge Two Binary Trees)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...

  5. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  6. 【leetcode刷题笔记】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. leetcode刷题-559. Maximum Depth of N-ary Tree

    题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...

  8. [leetcode刷题笔记]Implement Trie (Prefix Tree)

    题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...

  9. 【leetcode刷题笔记】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. python3利用cryptography 进行加密和解密

    我们的日常工作中,一定会遇到需要加密的数据,比如:密码.私密信息... ... 我们不仅要对他们进行加密,更需要对他们进行解密,因为毕竟我们的用户应该不会看得懂加密过后的字符串吧!!! 在python ...

  2. java中AQS源码分析

    AQS内部采用CLH队列.CLH队列是由节点组成.内部的Node节点包含的状态有 static final int CANCELLED =  1; static final int SIGNAL    ...

  3. 转自fineui论坛:解决fineui框架开发中的Designer.aspx.cs丢失问题

    在开发的时候碰到个问题,本来好好的Edit.aspx  Edit.aspx.cs  Edit.Designer.aspx.cs编辑Edit.aspx然后保存,编译的时候 发现Edit.aspx.cs里 ...

  4. 查看锁信息 v$lock 和 v$locked_object

    查看锁住的对象及会话id,serial# select a.*  from (SELECT o.object_name,               l.locked_mode,            ...

  5. SDS——重用StringBuilder

    package example.java; /** * @author 杜科 * @description 简单动态字符串,非线程安全.采取类似buffer的设计,使其成为一个可以方便重用的Strin ...

  6. 感知机算法(PLA)代码实现

    目录 1. 引言 2. 载入库和数据处理 3. 感知机的原始形式 4. 感知机的对偶形式 5. 多分类情况-one vs. rest 6. 多分类情况-one vs. one 7. sklearn实现 ...

  7. R语言 循环语句、分支语句和中止语句-控制流篇

    for 循环 用法 for (n in m) expr 若n在m中则运行 expr while 循环 用法 while (condition) expr 当符合condition时运行expr rep ...

  8. 使用 Azure Blob Stoage 实现一个静态文件服务器

    什么是Azure Blob Stoage Azure Blob Stoage 是微软Azure的对象存储服务.国内的云一般叫OSS,是一种用来存储非结构化数据的服务,比如音频,视频,图片,文本等等.用 ...

  9. 对于Javaweb初学者的一些坑。#Javaweb

    1.在配置好Tomcat之后 ,编译阶段发现报错 这种对于我个人来说一般有两种情况: ①在编写代码时(比如servlet)发现爆红,一般是maven的依赖没有导入,这个时候在xml文件中导入需要的包的 ...

  10. smartSVN9.2.2 for mac 安装与破解

    原文链接:https://www.jianshu.com/p/bb87154e0459 近段时间使用svn进行项目管理,开始使用的是cornerstone,但是用过程中出现一个操作Bug,一.在xco ...