mirror of
https://gitlab.com/game-loader/hugo.git
synced 2025-04-20 05:52:07 +08:00
add content to leetcode
This commit is contained in:
parent
a393f9719a
commit
2f90bd5ed1
@ -125,3 +125,85 @@ func findBottomLeftValue(root *TreeNode) int {
|
||||
### 总结
|
||||
|
||||
在题解中看到了一个使用深度优先搜索的方法, 记录当前搜索到的层级, 始终保存最大层级的第一个被搜索到的值, 因为使用的是后序遍历, 则每次遇到的当前层大于保存的最大层级时, 该节点就为新的最大层级的第一个节点, 即题目中要求的最左值(leftmost). 算法时间复杂度为O(n)------只遍历一次所有节点.
|
||||
|
||||
## day3 2024-02-29
|
||||
|
||||
### 1609. Even Odd Tree
|
||||
|
||||
A binary tree is named Even-Odd if it meets the following conditions:
|
||||
|
||||
The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.
|
||||
For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
|
||||
For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
|
||||
Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.
|
||||
|
||||

|
||||
|
||||
### 题解
|
||||
|
||||
对二叉树的奇数层, 其节点从左到右是严格递减的(意味着有两个节点的值相同是不允许的), 偶数层是严格递增的. 仍然可以使用昨天题目的层序遍历的方法, 增加一个level变量记录当前层数, 对该层内的节点值进行判断是否符合奇数层和偶数层对应的条件即可.
|
||||
|
||||
### 代码
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func isEvenOddTree(root *TreeNode) bool {
|
||||
level := 0
|
||||
index := -1
|
||||
var value *TreeNode
|
||||
queue := []*TreeNode{root}
|
||||
saved := 0
|
||||
for len(queue) != 0{
|
||||
// remove visited nodes
|
||||
queue = queue[index+1:]
|
||||
index = 0
|
||||
if(level%2 == 0){
|
||||
for index, value = range queue{
|
||||
if(index == 0){
|
||||
saved = value.Val
|
||||
if(saved %2 != 1){return false}
|
||||
} else{
|
||||
if(value.Val <= saved || (value.Val%2) != 1){
|
||||
return false
|
||||
} else{
|
||||
saved = value.Val
|
||||
}
|
||||
}
|
||||
if(value.Left != nil){queue = append(queue, value.Left)}
|
||||
if(value.Right != nil){queue = append(queue, value.Right)}
|
||||
}
|
||||
level++
|
||||
} else{
|
||||
for index, value = range queue{
|
||||
if(index == 0){
|
||||
saved = value.Val
|
||||
if(saved %2 != 0){return false}
|
||||
} else{
|
||||
if(value.Val >= saved || value.Val %2 != 0){
|
||||
return false
|
||||
} else{
|
||||
saved = value.Val
|
||||
}
|
||||
}
|
||||
if(value.Left != nil){queue = append(queue, value.Left)}
|
||||
if(value.Right != nil){queue = append(queue, value.Right)}
|
||||
}
|
||||
level++
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
### 总结
|
||||
|
||||
go语言中的for range循环, 如果使用类似`for key, value := range list` 的形式, 那么key, value这两个变量都会在当前作用域下新建, 意味着即使在前面定义了key, key的值在循环结束后也不会被修改. 若想修改之前定义的key值, 需要将value也提前定义好并使用`=`而不是`:=`.
|
||||
|
||||
go语言中的for range循环时如果使用`:=`会新建两个变量, 然后将slice中的值复制给value变量, 将对应的index值赋值给key变量, 这意味着value变量不会指向数组中对应位置的地址, 而是一个不变的单独地址.
|
||||
|
Loading…
Reference in New Issue
Block a user