【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
【101-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.
题目大意
给定一棵树,推断它是否是对称的。
即树的左子树是否是其右子树的镜像。
解题思路
使用递归进行求解。先推断左右子结点是否相等,不等就返回false。相等就将左子结点的左子树与右子结果的右子结点进行比較操作,同一时候将左子结点的左子树与右子结点的左子树进行比較,仅仅有两个同一时候为真是才返回true。否则返回false。
代码实现
树结点类
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
算法实现类
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
} else {
return isSame(root.left, root.right);
}
}
private boolean isSame(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
} if (left != null && right == null || left == null && right != null){
return false;
} else {
return left.val == right.val && isSame(left.left, right.right) && isSame(left.right, right.left);
}
}
}
评測结果
点击图片,鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47333335】
【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- [LeetCode]题解(python):101 Symmetric tree
题目来源 https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror ...
- Symmetric Tree 对称树
判断一棵二叉树是否为对称的树.如 1 / \ 2 2 / \ / \ 3 4 4 3 观察上面的树可以看出:左子树的右子树等于右子树的左子树,左子树的左子树等于右子树的右子树. 首先可以使用递归.递归 ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
随机推荐
- [POI2010]Divine Divisor
[POI2010]Divine Divisor 题目大意: 给你\(m(m\le600)\)个数\(a_i(a_i\le10^{18})\).\(n=\prod a_i\).现在要你找到一个最大的\( ...
- Codeforces Round #345 (Div. 1) A - Watchmen 容斥
C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...
- #iOS问题记录#WKWebView 闪退异常
异常描述: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 问题描述 ...
- NFS迁移
Auth: Jin Date: 20140317 需求将NFS共享IP切换为192.168.201.221,通过192.168.201.0网段提供共享(10.0.0.0和192.168.201.0都能 ...
- 利用require.js实现javascript模块化加载
这种引入很看到很想死吧! <script src="1.js"></script> <script src="2.js">& ...
- PostgreSQL配置文件--其他
9 CLIENT CONNECTION DEFAULTS 9.1 Statement Behavior 9.1.1 search_path 字符型 默认:search_path = '"$u ...
- C语言-对一个结构体中的字段进行排序
这是帮别人做的一个题目,好久没有接触过C语言了.有点发怵,只是似乎找回点当时学C语言,做课程设计的感觉. 题目:定义一个数组(学生结构体数组),里面包括学号.姓名.身份证和三科学生成绩.要求写一个函数 ...
- http://www.blogjava.net/zJun/archive/2006/06/28/55511.html
http://www.blogjava.net/zJun/archive/2006/06/28/55511.html http://www.cnblogs.com/alipayhutu/archive ...
- @NotEmpty、@NotBlank、@NotNull区别
@NotEmpty 用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上
- 【云计算】Docker容器时间同步如何配置?
示例: # 3.8 配置时区.时钟同步 # configure timezone & ntp RUN echo ${TIME_ZONE} > /etc/timezone \ && ...