Symmetric Tree leetcode java
问题描述:
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的更多相关文章
- LeetCode算法题-Symmetric Tree(Java实现)
		
这是悦乐书的第163次更新,第165篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第22题(顺位题号是101).给定二叉树,检查它是否是自身的镜像(即,围绕其中心对称). ...
 - Symmetric Tree [LeetCode]
		
Problem description: http://oj.leetcode.com/problems/symmetric-tree/ Basic idea: Both recursive and ...
 - Symmetric Tree——LeetCode
		
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
 - Recover Binary Search Tree leetcode java
		
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
 - 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 ...
 - Balanced Binary Tree leetcode java
		
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
 - 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 ...
 - 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 ...
 - 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 ...
 
随机推荐
- (转)JPA & Restful
			
参考博客: JPA: https://www.jianshu.com/p/b6932740f3c0 https://shimo.im/docs/zOer2qMVEggbF33d/ Restful: h ...
 - P3301 [SDOI2013]方程
			
思路 容斥的挺好的练习题 对于第二个条件,可以直接使m减去suma2,使得第二个条件舍去,然后m再减去n,使得问题转化成有n1个变量要满足小于等于某个数的条件,其他的随便取,求整数解的个数 对n1,以 ...
 - Balloons
			
题目链接:http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=2401 类似求连通块的问题,可以参考紫书(P162 油田),对这两个人分别执行dfs. ...
 - 【ASP.NET】System.Web.Routing - RouteCollection Class
			
Provides a collection of routes for ASP.NET routing. The RouteCollection class provides methods that ...
 - Lintcode35-Reverse Linked List-Easy
			
35. Reverse Linked List Reverse a linked list. Example Example1:For linked list 1->2->3, the r ...
 - C#深入多线程
			
主线程: th = Thread.CurrentThread; //现在的线程为主线程 th.Name = "MainThread"; //set线程名字:主线程本身没有名字 th ...
 - Python学习笔记3-string
			
More on Modules and their Namespaces Suppose you've got a module "binky.py" which contains ...
 - _event_stop
			
EventId 事件ID TeamId 事件玩家分组,攻守(防守为1,进攻为2),自定义阵营(_faction表自定义阵营ID),公会(公会guid) StopType 结束事件需要满足的条件,枚举类 ...
 - Oracle 11G Client客户端安装
			
参考资料: http://www.cnblogs.com/jiguixin/archive/2011/09/09/2172672.html http://blog.csdn.net/lanchengx ...
 - php格式化数字输出number_format
			
<?php $num = 4999.944444; $formattedNum = number_format($num).PHP_EOL; echo $formattedNum; $forma ...