博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Symmetric Tree,对称树
阅读量:4316 次
发布时间:2019-06-06

本文共 872 字,大约阅读时间需要 2 分钟。

问题描述:

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

 

算法分析:和same tree比较像,都可以用递归方法来解决。

public class SymmeticTree {	public boolean isSymmetric(TreeNode root) 	{		if (root == null) 		{			return true;		}		return isSymmetric(root.left, root.right);	}	public boolean isSymmetric(TreeNode left, TreeNode right) 	{		if (left == null && right == null) 		{			return true;		}		else if ((left == null && right != null)				|| (right == null && left != null) || (left.val != right.val))		{			return false;		}		else		{			return isSymmetric(left.left, right.right)					&& isSymmetric(left.right, right.left);		}	}}

 

转载于:https://www.cnblogs.com/masterlibin/p/5903854.html

你可能感兴趣的文章
Spring Boot Docker 实战
查看>>
Div Vertical Menu ver3
查看>>
Git简明操作
查看>>
InnoDB为什么要使用auto_Increment
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
零零碎碎的知识
查看>>
文件转码重写到其他文件
查看>>
AC自动机模板
查看>>
python 基本语法
查看>>
git配置
查看>>
【hexo】01安装
查看>>
使用case语句给字体改变颜色
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
JSP九大内置对象及四个作用域
查看>>
ConnectionString 属性尚未初始化
查看>>
数据结构-栈 C和C++的实现
查看>>