Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81d4685

Browse files
committed
添加 0101.对称二叉树.md Scala版本
1 parent be7d6e1 commit 81d4685

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎problems/0101.对称二叉树.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,5 +725,25 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
725725
}
726726
```
727727

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+
728748
-----------------------
729749
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /