题目:
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]相关推荐

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  6. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

最新文章

  1. merge r语言daframe_R语言读取多个excel文件后合并:rbind/merge/cmd合并
  2. 驾照考试(科目三-大路)
  3. python 语言教程(3)变量之列表(List)
  4. 关于我国科技创新研究,以及创新成果的转化的思考
  5. Response. AppendHeader使用大全及文件下载.net函数使用注意点(转载)
  6. uniapp 子组件 props拿不到数据_来吧!一文彻底搞定Vue组件!
  7. php打开就执行url,php执行URL解析
  8. codevs3872 邮递员送信(SPFA)
  9. mysql navicat 多语句_使用Navicat多对多关系SQL语句在MySQL中实现
  10. python接口自动化测试(五)-其它(认证代理超时配置)
  11. python递归实现快速对一个给定字符串排序输出
  12. IDEA java 显示build目录
  13. python普通滑块验证码破解初级版
  14. java教程 doc,java 基础教程.doc
  15. python 100天 pdf 最新版_GitHub - Nolan2018/Python-100-Days: Python - 100天从新手到大师
  16. 被遗忘权_继续–被遗忘的声明
  17. 距离(distance)算法小结
  18. 苹果开发者账号的那些坑
  19. 冷静分析:Opteron优势和潜在问题 (也是完全从网上copy的)
  20. word怎么设置边距为80磅_Word排版不能忽视的「标尺」工具,6 种用法 80% 的人不知道!...

热门文章

  1. codeforces contest 1140(D~G)
  2. 秒杀多线程第三篇 原子操作 Interlocked系列函数
  3. ADO学习(一)基础理论
  4. mediasoup-demo 运行实战
  5. EventBus设计与实现分析——订阅者的注册
  6. 聊聊风口上的 eBPF
  7. Git之删除本地无用分支
  8. 阿里技术官最新总结一份105道Java面试题小册,看完我惊呆了
  9. 挖一挖曹大 holmes 的设计与实现
  10. 青蛙跳台(含变种)及汉诺塔递归,母牛生小牛