[leetcode]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:
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.
confused what "{1,#,2,3}"
means?
> read more on how binary tree is serialized on OJ.
代码:
递归方法
public class Symmetric_Tree { //javapublic class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) { val = x; }}//recursivelypublic boolean isSymmetric(TreeNode root) {if(root == null)return true;return isSymmetricTree(root.left, root.right);}public boolean isSymmetricTree(TreeNode left, TreeNode right){if(left == null && right == null)return true;if(left == null || right == null ||left.val != right.val)return false;boolean lr = isSymmetricTree(left.left, right.right);boolean rl = isSymmetricTree(left.right, right.left);return lr && rl;}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
[leetcode]Symmetric Tree相关推荐
- 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /** * Definition for a binary tree no ...
- LeetCode 101. Symmetric Tree
LeetCode 101. Symmetric Tree Solution1 参考<剑指offer>上的解法:https://blog.csdn.net/allenlzcoder/arti ...
- 【LeetCode从零单排】No100 Same Tree No101 Symmetric Tree
题目 1.same tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- [LeetCode] Binary Tree Paths - 二叉树基础系列题目
目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...
- Symmetric Tree (101)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 101. Symmetric Tree (C语言)
101. Symmetric Tree (C语言) 判断是否为左右镜面对称的二叉树 题目 Given the root of a binary tree, check whether it is a ...
- leetcode python3 简单题101. Symmetric Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...
- Leetcode: 101. Symmetric Tree
题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...
最新文章
- SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化
- 浏览器User-Agent的详细信息
- 第7章 Scrapy突破反爬虫的限制
- java常见的异常_Java常见的10个异常
- 如何搭建中国电信物联网云平台
- 999999999分解质因数
- iView组件+Django实现前后端分离上传图片
- 江苏省专转本计算机专业大类《计算机基础理论 1.2(二)小节习题答案》
- JavaScript原型详解(通俗易懂)
- Java怎么弹出cmd命令窗口_怎么在java里打开bat文件,并打开cmd窗口执行,举例一个完整代码。...
- VMware vSphere与vCenter许可及价格分析
- yesterday is a doog day+margesort到底应该怎么写+quicksort到底应该怎么写啊+冒泡到底应该怎么写+ I am a student
- 2022-2028年中国科幻行业竞争现状及投资策略研究报告
- 关于秒杀场景的全面分析总结
- Julia并行计算笔记(二)
- 计算机应用中的过程控制,计算机在过程控制中的应用
- 自媒体内容发布要求有哪些?
- 阿里取消“P”序列职级显示引热议,网友:P3、P4流下了感动的泪水
- 766. 托普利茨矩阵(javascript)766. Toeplitz Matrix
- 苹果测试充电速度软件,苹果iPhone8 Plus充电测试:快充真的快吗?