问题描述:

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. (转)JPA & Restful

    参考博客: JPA: https://www.jianshu.com/p/b6932740f3c0 https://shimo.im/docs/zOer2qMVEggbF33d/ Restful: h ...

  2. P3301 [SDOI2013]方程

    思路 容斥的挺好的练习题 对于第二个条件,可以直接使m减去suma2,使得第二个条件舍去,然后m再减去n,使得问题转化成有n1个变量要满足小于等于某个数的条件,其他的随便取,求整数解的个数 对n1,以 ...

  3. Balloons

    题目链接:http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=2401 类似求连通块的问题,可以参考紫书(P162 油田),对这两个人分别执行dfs. ...

  4. 【ASP.NET】System.Web.Routing - RouteCollection Class

    Provides a collection of routes for ASP.NET routing. The RouteCollection class provides methods that ...

  5. Lintcode35-Reverse Linked List-Easy

    35. Reverse Linked List Reverse a linked list. Example Example1:For linked list 1->2->3, the r ...

  6. C#深入多线程

    主线程: th = Thread.CurrentThread; //现在的线程为主线程 th.Name = "MainThread"; //set线程名字:主线程本身没有名字 th ...

  7. Python学习笔记3-string

    More on Modules and their Namespaces Suppose you've got a module "binky.py" which contains ...

  8. _event_stop

    EventId 事件ID TeamId 事件玩家分组,攻守(防守为1,进攻为2),自定义阵营(_faction表自定义阵营ID),公会(公会guid) StopType 结束事件需要满足的条件,枚举类 ...

  9. Oracle 11G Client客户端安装

    参考资料: http://www.cnblogs.com/jiguixin/archive/2011/09/09/2172672.html http://blog.csdn.net/lanchengx ...

  10. php格式化数字输出number_format

    <?php $num = 4999.944444; $formattedNum = number_format($num).PHP_EOL; echo $formattedNum; $forma ...