Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
题目:
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 isSame(root.left,root.right);}}public boolean isSame(TreeNode left,TreeNode right) {if(left==null&&right==null){return true;}else if(left!=null&&right!=null){if(left.val!=right.val){return false;}boolean res1=isSame(left.left,right.right);boolean res2=isSame(left.right,right.left);if(res1&&res2){return true;}else{return false;}}else{return false;}}
}
Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]相关推荐
- Leet Code OJ 100. Same Tree [Difficulty: Easy]
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Leet Code OJ 344. Reverse String [Difficulty: Easy]
题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...
- Leet Code OJ 28. Implement strStr() [Difficulty: Easy]
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
题目: Given a string containing just the characters , determine if the input string is valid. The brac ...
- Leet Code OJ 1. Two Sum [Difficulty: Easy]
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Leet Code OJ 223. Rectangle Area [Difficulty: Easy]
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- Leet Code OJ 189. Rotate Array [Difficulty: Easy]
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
最新文章
- merge r语言daframe_R语言读取多个excel文件后合并:rbind/merge/cmd合并
- 驾照考试(科目三-大路)
- python 语言教程(3)变量之列表(List)
- 关于我国科技创新研究,以及创新成果的转化的思考
- Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
- uniapp 子组件 props拿不到数据_来吧!一文彻底搞定Vue组件!
- php打开就执行url,php执行URL解析
- codevs3872 邮递员送信(SPFA)
- mysql navicat 多语句_使用Navicat多对多关系SQL语句在MySQL中实现
- python接口自动化测试(五)-其它(认证代理超时配置)
- python递归实现快速对一个给定字符串排序输出
- IDEA java 显示build目录
- python普通滑块验证码破解初级版
- java教程 doc,java 基础教程.doc
- python 100天 pdf 最新版_GitHub - Nolan2018/Python-100-Days: Python - 100天从新手到大师
- 被遗忘权_继续–被遗忘的声明
- 距离(distance)算法小结
- 苹果开发者账号的那些坑
- 冷静分析:Opteron优势和潜在问题 (也是完全从网上copy的)
- word怎么设置边距为80磅_Word排版不能忽视的「标尺」工具,6 种用法 80% 的人不知道!...