LeetCode_101. Symmetric Tree_对称二叉树
2016-01-19 20:12
477 查看
101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
But the following is not:
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 class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null) {
return true;
}else{
return MirrorTree(root.left,root.right);
}
}
private boolean MirrorTree(TreeNode left,TreeNode right){
if (left == null && right == null)
return true;
if (left == null || right == null)
return false;
if (left.val != right.val)
return false;
return MirrorTree(left.left, right.right) && MirrorTree(left.right, right.left);
}
}
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 class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null) {
return true;
}else{
return MirrorTree(root.left,root.right);
}
}
private boolean MirrorTree(TreeNode left,TreeNode right){
if (left == null && right == null)
return true;
if (left == null || right == null)
return false;
if (left.val != right.val)
return false;
return MirrorTree(left.left, right.right) && MirrorTree(left.right, right.left);
}
}
相关文章推荐
- NYOJ1170(最大的数)(贪心?)
- 谷歌C++编程规范笔记
- Androidx学习笔记(8)--常见布局--帧布局
- leetcode 之 same tree
- 架构一个后台管理系统的技术所需要的技术
- Android使用fragment切换,界面间传值操作
- /proc/net/vlan
- Android Studio 插件简介
- C++之智能指针 (一 )
- 按照层序,从左到右遍历
- bzoj:1187: [HNOI2007]神奇游乐园
- ASPNET5 依赖注入(Dependency Injection)
- 微服务与SOA的实践应用对比
- Android 事件分发keyEvent、touchEvent、hoverEvent
- Sql练习15题
- 2H开发板启动 android 4.0.3 系统
- Super和this的应用
- epoll
- Python【基础第一篇】
- 蓄电池浮充电和均充电