问题描述:

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. 解决Visual Studio(2017)软件无法重新生成问题

    https://blog.csdn.net/qq_38265674/article/details/80539228 笔者用VS2017打开VS2015创建的工程,出现如下图的问题. 不小心没有升级平 ...

  2. js 二叉树删除最大值和最小值

    //删除最小值function delMinNode (root){ if(!root) { return false; } var current = root; if (current.left ...

  3. promise对象的使用

    ES6中的promise的出现给我们很好的解决了回调地狱的问题,在使用ES5的时候,在多层嵌套回调时,写完的代码层次过多,很难进行维护和二次开发,ES6认识到了这点问题, 现在promise的使用,完 ...

  4. selenium 在电脑浏览器中用手机模式打开

    import requests from selenium import webdriver from selenium.webdriver.common.action_chains import A ...

  5. win10,配置python3.6,虚拟环境

    1.安装python3.6(官网下载) 2.pip install virtualenv(安装虚拟环境) 3.virtualenv TestEnv(创建名为TestEnv的虚拟环境) 4.进入Test ...

  6. 封装sqlhelper【一】

    控件信息展示: //定义调用数据库类文件 namespace SqlHelper { public class TblClass { public int classId { get; set; } ...

  7. 记录flask使用模板时出现的“Internal Server Error”错误

    在看<Flask Web开发实战:入门.进阶与原理解析(李辉著 )>时照着书上的代码抄了一遍,然后运行时发现一直出现以下的错误 书上的源代码如下 watchlist.html <he ...

  8. Android主页Activity对多个Fragment实现不同的沉浸式标题(图片或者文字标题)

    提示:讲解的该例实现是 FragmentTabHost + Fragment 实现: 1.示例效果图: 2.场景需求: 如示例图所示,在首页实现轮播图的沉浸,而 “发现” 和“我的”页是标题的沉浸. ...

  9. Zabbix报警执行远程命令

    日常Zabbix报警案例中,大多都是报警发送邮件,场景里很少有需要报警后执行一个命令(启动服务.清空磁盘空间.关停服务器);而今天就给大家讲讲最近需要做的事:报警后执行远程命令 刚好zabbix动作中 ...

  10. 《剑指offer》第四十二题(连续子数组的最大和)

    // 面试题42:连续子数组的最大和 // 题目:输入一个整型数组,数组里有正数也有负数.数组中一个或连续的多个整 // 数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为O(n). #in ...