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 #22

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 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jun 23, 2022
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
8f9c8e5
1049.最后一块石头重量 新增typescript版本代码
Jerry-306 May 23, 2022
ffc91f1
0494.目标和 新增typescript版本代码
Jerry-306 May 23, 2022
282cdc2
添加(1365.有多少小于当前数字的数字.md):增加typescript版本
xiaofei-2020 May 23, 2022
f83d5ed
添加 0222.完全二叉树的节点个数.md Scala版本
wzqwtt May 23, 2022
45a3c91
添加 0257.二叉树的所有路径.md Scala版本
wzqwtt May 23, 2022
1a89d3c
Merge branch 'master' into patch-60
youngyangyang04 Jun 22, 2022
6bf645a
Merge pull request #1388 from Jerry-306/patch-60
youngyangyang04 Jun 23, 2022
e7e03af
Merge branch 'master' into patch-61
youngyangyang04 Jun 23, 2022
a0105bf
Merge pull request #1389 from Jerry-306/patch-61
youngyangyang04 Jun 23, 2022
abe1ec3
Merge pull request #1390 from xiaofei-2020/extra01
youngyangyang04 Jun 23, 2022
a1e9f25
Merge pull request #1391 from wzqwtt/tree08
youngyangyang04 Jun 23, 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
Prev Previous commit
Next Next commit
添加 0222.完全二叉树的节点个数.md Scala版本
  • Loading branch information
wzqwtt committed May 23, 2022
commit f83d5edb6ebf5cd6de1eabba51a246a921f94e1b
63 changes: 63 additions & 0 deletions problems/0222.完全二叉树的节点个数.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -646,5 +646,68 @@ func countNodes(_ root: TreeNode?) -> Int {
}
```

## Scala

递归:
```scala
object Solution {
def countNodes(root: TreeNode): Int = {
if(root == null) return 0
1 + countNodes(root.left) + countNodes(root.right)
}
}
```

层序遍历:
```scala
object Solution {
import scala.collection.mutable
def countNodes(root: TreeNode): Int = {
if (root == null) return 0
val queue = mutable.Queue[TreeNode]()
var node = 0
queue.enqueue(root)
while (!queue.isEmpty) {
val len = queue.size
for (i <- 0 until len) {
node += 1
val curNode = queue.dequeue()
if (curNode.left != null) queue.enqueue(curNode.left)
if (curNode.right != null) queue.enqueue(curNode.right)
}
}
node
}
}
```

利用完全二叉树性质:
```scala
object Solution {
def countNodes(root: TreeNode): Int = {
if (root == null) return 0
var leftNode = root.left
var rightNode = root.right
// 向左向右往下探
var leftDepth = 0
while (leftNode != null) {
leftDepth += 1
leftNode = leftNode.left
}
var rightDepth = 0
while (rightNode != null) {
rightDepth += 1
rightNode = rightNode.right
}
// 如果相等就是一个满二叉树
if (leftDepth == rightDepth) {
return (2 << leftDepth) - 1
}
// 如果不相等就不是一个完全二叉树,继续向下递归
countNodes(root.left) + countNodes(root.right) + 1
}
}
```

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

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