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

[pull] master from youngyangyang04:master #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 19 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
568a569
添加(1143.最长公共子序列.md):增加typescript版本
xiaofei-2020 May 19, 2022
b3335f7
添加 763.划分字母区间 补充思路的Java代码实现
May 19, 2022
30c1be1
添加(1035.不相交的线.md):增加typescript版本
xiaofei-2020 May 19, 2022
4d2b80c
添加(0053.最大子序和动态规划.md):增加typescript版本
xiaofei-2020 May 19, 2022
8759349
添加(0392.判断子序列.md):增加typescript版本
xiaofei-2020 May 19, 2022
4eefc54
添加(0115.不同的子序列.md):增加typescript版本
xiaofei-2020 May 20, 2022
58c4ad4
添加(0583.两个字符串的删除操作.md):增加typescript版本
xiaofei-2020 May 20, 2022
fa3a45c
添加(0072.编辑距离.md):增加typescript版本
xiaofei-2020 May 20, 2022
be7d6e1
添加 0226.翻转二叉树.md Scala版本
wzqwtt May 20, 2022
81d4685
添加 0101.对称二叉树.md Scala版本
wzqwtt May 20, 2022
7163601
Merge pull request #1358 from xiaofei-2020/dp44
youngyangyang04 Jun 12, 2022
a054544
Merge pull request #1359 from zhouchaoyu1/master
youngyangyang04 Jun 13, 2022
6b8692c
Merge pull request #1361 from xiaofei-2020/dp45
youngyangyang04 Jun 13, 2022
3071e3b
Merge pull request #1362 from xiaofei-2020/dp46
youngyangyang04 Jun 13, 2022
7886da3
Merge pull request #1364 from xiaofei-2020/dp47
youngyangyang04 Jun 13, 2022
9727003
Merge pull request #1366 from xiaofei-2020/dp48
youngyangyang04 Jun 13, 2022
6d130e7
Merge pull request #1367 from xiaofei-2020/dp49
youngyangyang04 Jun 13, 2022
f9aeae5
Merge pull request #1368 from xiaofei-2020/dp50
youngyangyang04 Jun 13, 2022
e9cdb3e
Merge pull request #1369 from wzqwtt/tree06
youngyangyang04 Jun 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions problems/0101.对称二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -725,5 +725,25 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
}
```

## Scala

递归:
```scala
object Solution {
def isSymmetric(root: TreeNode): Boolean = {
if (root == null) return true // 如果等于空直接返回true
def compare(left: TreeNode, right: TreeNode): Boolean = {
if (left == null && right == null) return true // 如果左右都为空,则为true
if (left == null && right != null) return false // 如果左空右不空,不对称,返回false
if (left != null && right == null) return false // 如果左不空右空,不对称,返回false
// 如果左右的值相等,并且往下递归
left.value == right.value && compare(left.left, right.right) && compare(left.right, right.left)
}
// 分别比较左子树和右子树
compare(root.left, root.right)
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
48 changes: 48 additions & 0 deletions problems/0226.翻转二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,53 @@ func invertTree(_ root: TreeNode?) -> TreeNode? {
}
```

### Scala

深度优先遍历(前序遍历):
```scala
object Solution {
def invertTree(root: TreeNode): TreeNode = {
if (root == null) return root
// 递归
def process(node: TreeNode): Unit = {
if (node == null) return
// 翻转节点
val curNode = node.left
node.left = node.right
node.right = curNode
process(node.left)
process(node.right)
}
process(root)
root
}
}
```

广度优先遍历(层序遍历):
```scala
object Solution {
import scala.collection.mutable
def invertTree(root: TreeNode): TreeNode = {
if (root == null) return root
val queue = mutable.Queue[TreeNode]()
queue.enqueue(root)
while (!queue.isEmpty) {
val len = queue.size
for (i <- 0 until len) {
var curNode = queue.dequeue()
if (curNode.left != null) queue.enqueue(curNode.left)
if (curNode.right != null) queue.enqueue(curNode.right)
// 翻转
var tmpNode = curNode.left
curNode.left = curNode.right
curNode.right = tmpNode
}
}
root
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

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