mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -496,8 +496,26 @@ struct ListNode* reverseList(struct ListNode* head){
|
||||
return reverse(NULL, head);
|
||||
}
|
||||
```
|
||||
Scala:
|
||||
|
||||
|
||||
|
||||
PHP:
|
||||
```php
|
||||
// 双指针法:
|
||||
function reverseList($head) {
|
||||
$cur = $head;
|
||||
$pre = NULL;
|
||||
while($cur){
|
||||
$temp = $cur->next;
|
||||
$cur->next = $pre;
|
||||
$pre = $cur;
|
||||
$cur = $temp;
|
||||
}
|
||||
return $pre;
|
||||
}
|
||||
```
|
||||
|
||||
Scala:
|
||||
双指针法:
|
||||
```scala
|
||||
object Solution {
|
||||
@ -529,6 +547,7 @@ object Solution {
|
||||
cur.next = pre
|
||||
reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
-----------------------
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
Reference in New Issue
Block a user