We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent be7d6e1 commit 81d4685Copy full SHA for 81d4685
problems/0101.对称二叉树.md
@@ -725,5 +725,25 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
725
}
726
```
727
728
+## Scala
729
+
730
+递归:
731
+```scala
732
+object Solution {
733
+ def isSymmetric(root: TreeNode): Boolean = {
734
+ if (root == null) return true // 如果等于空直接返回true
735
+ def compare(left: TreeNode, right: TreeNode): Boolean = {
736
+ if (left == null && right == null) return true // 如果左右都为空,则为true
737
+ if (left == null && right != null) return false // 如果左空右不空,不对称,返回false
738
+ if (left != null && right == null) return false // 如果左不空右空,不对称,返回false
739
+ // 如果左右的值相等,并且往下递归
740
+ left.value == right.value && compare(left.left, right.right) && compare(left.right, right.left)
741
+ }
742
+ // 分别比较左子树和右子树
743
+ compare(root.left, root.right)
744
745
+}
746
+```
747
748
-----------------------
749
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments