mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1397 from wzqwtt/tree09
添加(0404.左叶子之和、0513.找树左下角的值)Scala版本
This commit is contained in:
@ -516,6 +516,44 @@ int sumOfLeftLeaves(struct TreeNode* root){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
**递归:**
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def sumOfLeftLeaves(root: TreeNode): Int = {
|
||||||
|
if(root == null) return 0
|
||||||
|
var midValue = 0
|
||||||
|
if(root.left != null && root.left.left == null && root.left.right == null){
|
||||||
|
midValue = root.left.value
|
||||||
|
}
|
||||||
|
// return关键字可以省略
|
||||||
|
midValue + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**迭代:**
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def sumOfLeftLeaves(root: TreeNode): Int = {
|
||||||
|
val stack = mutable.Stack[TreeNode]()
|
||||||
|
if (root == null) return 0
|
||||||
|
stack.push(root)
|
||||||
|
var sum = 0
|
||||||
|
while (!stack.isEmpty) {
|
||||||
|
val curNode = stack.pop()
|
||||||
|
if (curNode.left != null && curNode.left.left == null && curNode.left.right == null) {
|
||||||
|
sum += curNode.left.value // 如果满足条件就累加
|
||||||
|
}
|
||||||
|
if (curNode.right != null) stack.push(curNode.right)
|
||||||
|
if (curNode.left != null) stack.push(curNode.left)
|
||||||
|
}
|
||||||
|
sum
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -546,7 +546,50 @@ func findBottomLeftValue(_ root: TreeNode?) -> Int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
递归版本:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def findBottomLeftValue(root: TreeNode): Int = {
|
||||||
|
var maxLeftValue = 0
|
||||||
|
var maxLen = Int.MinValue
|
||||||
|
// 递归方法
|
||||||
|
def traversal(node: TreeNode, leftLen: Int): Unit = {
|
||||||
|
// 如果左右都为空并且当前深度大于最大深度,记录最左节点的值
|
||||||
|
if (node.left == null && node.right == null && leftLen > maxLen) {
|
||||||
|
maxLen = leftLen
|
||||||
|
maxLeftValue = node.value
|
||||||
|
}
|
||||||
|
if (node.left != null) traversal(node.left, leftLen + 1)
|
||||||
|
if (node.right != null) traversal(node.right, leftLen + 1)
|
||||||
|
}
|
||||||
|
traversal(root, 0) // 调用方法
|
||||||
|
maxLeftValue // return关键字可以省略
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
层序遍历:
|
||||||
|
```scala
|
||||||
|
import scala.collection.mutable
|
||||||
|
|
||||||
|
def findBottomLeftValue(root: TreeNode): Int = {
|
||||||
|
val queue = mutable.Queue[TreeNode]()
|
||||||
|
queue.enqueue(root)
|
||||||
|
var res = 0 // 记录每层最左侧结果
|
||||||
|
while (!queue.isEmpty) {
|
||||||
|
val len = queue.size
|
||||||
|
for (i <- 0 until len) {
|
||||||
|
val curNode = queue.dequeue()
|
||||||
|
if (i == 0) res = curNode.value // 记录最最左侧的节点
|
||||||
|
if (curNode.left != null) queue.enqueue(curNode.left)
|
||||||
|
if (curNode.right != null) queue.enqueue(curNode.right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res // 最终返回结果,return关键字可以省略
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user