日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]

發布時間:2024/2/28 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。