Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
生活随笔
收集整理的這篇文章主要介紹了
Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目:
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.
翻譯:
給定一個(gè)二叉樹,檢測(cè)它是不是一個(gè)它自己的鏡像(左右對(duì)稱)。
代碼:
/*** 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;}} }總結(jié)
以上是生活随笔為你收集整理的Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leet Code OJ 202. Ha
- 下一篇: Leet Code OJ 112. Pa