mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1381 from wzqwtt/tree07
添加(0104.二叉树的最大深度、0111.二叉树的最小深度)Scala版本
This commit is contained in:
@ -495,7 +495,7 @@ class solution:
|
|||||||
|
|
||||||
|
|
||||||
## go
|
## go
|
||||||
|
### 104.二叉树的最大深度
|
||||||
```go
|
```go
|
||||||
/**
|
/**
|
||||||
* definition for a binary tree node.
|
* definition for a binary tree node.
|
||||||
@ -548,6 +548,8 @@ func maxdepth(root *treenode) int {
|
|||||||
|
|
||||||
## javascript
|
## javascript
|
||||||
|
|
||||||
|
### 104.二叉树的最大深度
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var maxdepth = function(root) {
|
var maxdepth = function(root) {
|
||||||
if (root === null) return 0;
|
if (root === null) return 0;
|
||||||
@ -595,6 +597,8 @@ var maxDepth = function(root) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 559.n叉树的最大深度
|
||||||
|
|
||||||
N叉树的最大深度 递归写法
|
N叉树的最大深度 递归写法
|
||||||
```js
|
```js
|
||||||
var maxDepth = function(root) {
|
var maxDepth = function(root) {
|
||||||
@ -627,9 +631,9 @@ var maxDepth = function(root) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
## TypeScript:
|
## TypeScript
|
||||||
|
|
||||||
> 二叉树的最大深度:
|
### 104.二叉树的最大深度
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 后续遍历(自下而上)
|
// 后续遍历(自下而上)
|
||||||
@ -672,7 +676,7 @@ function maxDepth(root: TreeNode | null): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
> N叉树的最大深度
|
### 559.n叉树的最大深度
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 后续遍历(自下而上)
|
// 后续遍历(自下而上)
|
||||||
@ -702,6 +706,8 @@ function maxDepth(root: TreeNode | null): number {
|
|||||||
|
|
||||||
## C
|
## C
|
||||||
|
|
||||||
|
### 104.二叉树的最大深度
|
||||||
|
|
||||||
二叉树最大深度递归
|
二叉树最大深度递归
|
||||||
```c
|
```c
|
||||||
int maxDepth(struct TreeNode* root){
|
int maxDepth(struct TreeNode* root){
|
||||||
@ -758,7 +764,8 @@ int maxDepth(struct TreeNode* root){
|
|||||||
|
|
||||||
## Swift
|
## Swift
|
||||||
|
|
||||||
>二叉树最大深度
|
### 104.二叉树的最大深度
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
// 递归 - 后序
|
// 递归 - 后序
|
||||||
func maxDepth1(_ root: TreeNode?) -> Int {
|
func maxDepth1(_ root: TreeNode?) -> Int {
|
||||||
@ -797,7 +804,8 @@ func maxDepth(_ root: TreeNode?) -> Int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
>N叉树最大深度
|
### 559.n叉树的最大深度
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
// 递归
|
// 递归
|
||||||
func maxDepth(_ root: Node?) -> Int {
|
func maxDepth(_ root: Node?) -> Int {
|
||||||
@ -833,5 +841,84 @@ func maxDepth1(_ root: Node?) -> Int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
### 104.二叉树的最大深度
|
||||||
|
递归法:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def maxDepth(root: TreeNode): Int = {
|
||||||
|
def process(curNode: TreeNode): Int = {
|
||||||
|
if (curNode == null) return 0
|
||||||
|
// 递归左节点和右节点,返回最大的,最后+1
|
||||||
|
math.max(process(curNode.left), process(curNode.right)) + 1
|
||||||
|
}
|
||||||
|
// 调用递归方法,return关键字可以省略
|
||||||
|
process(root)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代法:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def maxDepth(root: TreeNode): Int = {
|
||||||
|
var depth = 0
|
||||||
|
if (root == null) return depth
|
||||||
|
val queue = mutable.Queue[TreeNode]()
|
||||||
|
queue.enqueue(root)
|
||||||
|
while (!queue.isEmpty) {
|
||||||
|
val len = queue.size
|
||||||
|
for (i <- 0 until len) {
|
||||||
|
val curNode = queue.dequeue()
|
||||||
|
if (curNode.left != null) queue.enqueue(curNode.left)
|
||||||
|
if (curNode.right != null) queue.enqueue(curNode.right)
|
||||||
|
}
|
||||||
|
depth += 1 // 只要有层次就+=1
|
||||||
|
}
|
||||||
|
depth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 559.n叉树的最大深度
|
||||||
|
|
||||||
|
递归法:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def maxDepth(root: Node): Int = {
|
||||||
|
if (root == null) return 0
|
||||||
|
var depth = 0
|
||||||
|
for (node <- root.children) {
|
||||||
|
depth = math.max(depth, maxDepth(node))
|
||||||
|
}
|
||||||
|
depth + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代法: (层序遍历)
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def maxDepth(root: Node): Int = {
|
||||||
|
if (root == null) return 0
|
||||||
|
var depth = 0
|
||||||
|
val queue = mutable.Queue[Node]()
|
||||||
|
queue.enqueue(root)
|
||||||
|
while (!queue.isEmpty) {
|
||||||
|
val len = queue.size
|
||||||
|
depth += 1
|
||||||
|
for (i <- 0 until len) {
|
||||||
|
val curNode = queue.dequeue()
|
||||||
|
for (node <- curNode.children) queue.enqueue(node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
depth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<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>
|
||||||
|
@ -488,6 +488,46 @@ func minDepth(_ root: TreeNode?) -> Int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## Scala
|
||||||
|
|
||||||
|
递归法:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def minDepth(root: TreeNode): Int = {
|
||||||
|
if (root == null) return 0
|
||||||
|
if (root.left == null && root.right != null) return 1 + minDepth(root.right)
|
||||||
|
if (root.left != null && root.right == null) return 1 + minDepth(root.left)
|
||||||
|
// 如果两侧都不为空,则取最小值,return关键字可以省略
|
||||||
|
1 + math.min(minDepth(root.left), minDepth(root.right))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代法:
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def minDepth(root: TreeNode): Int = {
|
||||||
|
if (root == null) return 0
|
||||||
|
var depth = 0
|
||||||
|
val queue = mutable.Queue[TreeNode]()
|
||||||
|
queue.enqueue(root)
|
||||||
|
while (!queue.isEmpty) {
|
||||||
|
depth += 1
|
||||||
|
val len = queue.size
|
||||||
|
for (i <- 0 until len) {
|
||||||
|
val curNode = queue.dequeue()
|
||||||
|
if (curNode.left != null) queue.enqueue(curNode.left)
|
||||||
|
if (curNode.right != null) queue.enqueue(curNode.right)
|
||||||
|
if (curNode.left == null && curNode.right == null) return depth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
depth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
rust:
|
rust:
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -550,6 +590,7 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
min_depth
|
min_depth
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user