博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
101. Symmetric Tree.md
阅读量:3574 次
发布时间:2019-05-20

本文共 726 字,大约阅读时间需要 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
Note:
Bonus points if you could solve it both recursively and iteratively.

递归求解

代码如下:

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

转载地址:http://fnjgj.baihongyu.com/

你可能感兴趣的文章
实现自己的权限管理系统(十三):redis做缓存
查看>>
实现自己的权限管理系统(十四):工具类
查看>>
JavaWeb面经(二):2019.9.16 Synchronized关键字底层原理及作用
查看>>
牛客的AI模拟面试(1)
查看>>
深入浅出MyBatis:MyBatis解析和运行原理
查看>>
Mybatis与Ibatis
查看>>
字节码文件(Class文件)
查看>>
java中的IO流(一)----概述
查看>>
StringBuilder
查看>>
集合,Collection
查看>>
泛型详解
查看>>
泛型实现斗地主
查看>>
List集合
查看>>
ArrayList集合,LinkedList集合,Vector集合
查看>>
HashSet集合
查看>>
并发与并行,线程与进程
查看>>
方法引用,通过对象名引用成员变量
查看>>
常用工具类 Math:数学计算 Random:生成伪随机数 SecureRandom:生成安全的随机数 2020-2-13
查看>>
Java的异常Exception 2020-2-13
查看>>
Java标准库定义的常用异常,自定义异常 2020-2-15
查看>>