问题描述:

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

For example, this binary tree is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:

    1
/ \
2 2
\ \
3 3

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

分析:判断给定的一棵二叉树是不是对称的。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
 public boolean isSymmetric(TreeNode root) { //递归
if (root == null) return true;
return isMiror(root.left, root.right);
}
public boolean isMiror(TreeNode n1, TreeNode n2) { //判断两棵树是否成镜像
if (n1 == null && n2 == null) return true;
if (n1 == null && n2 != null) return false;
if (n1 != null && n2 == null) return false;
if (n1.val != n2.val) return false;
return isMiror(n1.left, n2.right) && isMiror(n1.right, n2.left);
}

Symmetric Tree leetcode java的更多相关文章

  1. LeetCode算法题-Symmetric Tree(Java实现)

    这是悦乐书的第163次更新,第165篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第22题(顺位题号是101).给定二叉树,检查它是否是自身的镜像(即,围绕其中心对称). ...

  2. Symmetric Tree [LeetCode]

    Problem description: http://oj.leetcode.com/problems/symmetric-tree/ Basic idea: Both recursive and ...

  3. Symmetric Tree——LeetCode

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

  4. Recover Binary Search Tree leetcode java

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  5. Validate Binary Search Tree leetcode java

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  6. Balanced Binary Tree leetcode java

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  7. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  8. Minimum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  9. Maximum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

随机推荐

  1. 【系列教程1】Gradle入门系列三:依赖管理

    在现实生活中,要创造一个没有任何外部依赖的应用程序并非不可能,但也是极具挑战的.这也是为什么依赖管理对于每个软件项目都是至关重要的一部分. 这篇教程主要讲述如何使用Gradle管理我们项目的依赖,我们 ...

  2. awk - group adjacent rows by identical columns

    Liang always brings me interesting quiz questions. Here is one: If i have a table like below: chr1 1 ...

  3. orcl 之 导入和导出(2)

    删除用户表,表数据等内容 drop user username cascade 无法为表空间 spancename 中的段创建INITAL区处理办法 找到表空间存储的地方文件 select file_ ...

  4. wamp phpcms部署网站问题

    服务器使用自己的服务器,域名申请后通过信息服务iis管理器建网站,并将物理地址指定到wamp的www目录中的网站的根目录.这时候如果网站首页已经生成后访问域名将进入网站首页. 出现的问题: 1.导航( ...

  5. ThreadLocal使用

    ThreadLocal提供了一种访问某个变量的特殊方式:访问到的变量属于当前线程,即保证每个线程的变量不一样,而同一个线程在任何地方拿到的变量都是一致的,这就是所谓的线程隔离. 如果要使用Thread ...

  6. 17秋 SDN课程 第三次上机作业

    SDN 第三次上机作业 1.创建拓扑 2.利用OVS命令下发流表,实现vlan功能 3.利用OVS命令查看流表 s1: s2: 4.验证性测试 5.Wireshark 抓包验证

  7. [小问题笔记(九)] SQL语句Not IN 效率低,用 NOT EXISTS试试

    项目中遇到这么个情况: t1表 和 t2表  都是150w条数据,600M的样子,都不算大. 但是这样一句查询 ↓ select * from t1 where phone not in (selec ...

  8. 【Python】【面向对象】

    """# [[面向对象]]#[访问限制]#如果要让内部属性不被外部访问,可加双下划线,编程私有变量.只有内部可以访问,外部不能访问.class Student(objec ...

  9. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo 矩阵快速幂优化dp

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  10. Qt5.QString传参数

    1.函数传参,如果是 QString&类型 的话,不能直接 传入 char* 类型的参数,若是声明成 const QString&类型 的话,就可以 解释:应该是 函数调用的时候 编译 ...