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 ...
随机推荐
- 【做题】BZOJ2342 双倍回文——马拉车&并查集
题意:有一个长度为\(n\)的字符串,求它最长的子串\(s\)满足\(s\)是长度为4的倍数的回文串,且它的前半部分和后半部分都是回文串. \(n \leq 5 \times 10^5\) 首先,显然 ...
- 多线程编程:一个指令重排序引发的chaos
先贴出正确的代码: package com.xiaobai.thread.main; import lombok.extern.slf4j.Slf4j; @Slf4j public class Thr ...
- 用.native修饰器来对外部组件进行构造器内部方法的调用以及用原生js获取构造器里的方法
html <div id="app"> <span v-text="number"></span> <btn @cli ...
- Shell: nohup守护进程化
如果想在终端会话中启动shell脚本,然后让脚本一直以后台模式运行,直到其完成,即使你退出了终端会话,可以使用nohup命令实现.感觉nohup就是将一个进程初始化为一个守护进程. nohup命令运行 ...
- HDU 3526 Computer Assembling(最小割)
http://acm.hdu.edu.cn/showproblem.php?pid=3526 题意:有个屌丝要配置电脑,现在有n个配件需要购买,有两家公司出售这n个配件,还有m个条件是如果配件x和配件 ...
- JavaScript基本内容
注释: /*多行 注释*/ //单行注释 变量: //变量均为对象,常用类型:String.Number.Boolean.Array.Object var value = "hello&qu ...
- BZOJ 4826 【HNOI2017】 影魔
题目链接:影魔 这道题就是去年序列的弱化版啊…… 我们枚举最大值的位置\(i\),找出左边第一个比\(a_i\)大的位置\(l\),右边第一个比\(a_i\)大的位置\(r\),然后我们分开考虑一下\ ...
- 【BZOJ】3295: [Cqoi2011]动态逆序对
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3295 mamaya,弱鸡xrdog终于会写树套树啦.... 将树状数组中每一个节点看成一棵 ...
- hdu 1115 Lifting the Stone 多边形的重心
Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Spring (一)
Spring是一个开源框架,是一个基于IOC和AOP来架构多层的JavaEE 架构 默认是单例模式 IOC就是 Inversion of Control public class Girl { pri ...