mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
update 0513.找树左下角的值 优化 go 和 js 代码
This commit is contained in:
@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
## 思路
|
## 思路
|
||||||
|
|
||||||
本地要找出树的最后一行找到最左边的值。此时大家应该想起用层序遍历是非常简单的了,反而用递归的话会比较难一点。
|
本题要找出树的最后一行的最左边的值。此时大家应该想起用层序遍历是非常简单的了,反而用递归的话会比较难一点。
|
||||||
|
|
||||||
我们依然还是先介绍递归法。
|
我们依然还是先介绍递归法。
|
||||||
|
|
||||||
@ -46,7 +46,7 @@
|
|||||||
|
|
||||||
所以要找深度最大的叶子节点。
|
所以要找深度最大的叶子节点。
|
||||||
|
|
||||||
那么如果找最左边的呢?可以使用前序遍历(当然中序,后序都可以,因为本题没有 中间节点的处理逻辑,只要左优先就行),保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。
|
那么如何找最左边的呢?可以使用前序遍历(当然中序,后序都可以,因为本题没有 中间节点的处理逻辑,只要左优先就行),保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。
|
||||||
|
|
||||||
递归三部曲:
|
递归三部曲:
|
||||||
|
|
||||||
@ -169,7 +169,7 @@ public:
|
|||||||
|
|
||||||
### 迭代法
|
### 迭代法
|
||||||
|
|
||||||
本题使用层序遍历再合适不过了,比递归要好理解的多!
|
本题使用层序遍历再合适不过了,比递归要好理解得多!
|
||||||
|
|
||||||
只需要记录最后一行第一个节点的数值就可以了。
|
只需要记录最后一行第一个节点的数值就可以了。
|
||||||
|
|
||||||
@ -323,34 +323,25 @@ class Solution:
|
|||||||
递归法:
|
递归法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var maxDeep int // 全局变量 深度
|
var depth int // 全局变量 最大深度
|
||||||
var value int //全局变量 最终值
|
var res int // 记录最终结果
|
||||||
func findBottomLeftValue(root *TreeNode) int {
|
func findBottomLeftValue(root *TreeNode) int {
|
||||||
if root.Left==nil&&root.Right==nil{//需要提前判断一下(不要这个if的话提交结果会出错,但执行代码不会。防止这种情况出现,故先判断是否只有一个节点)
|
depth, res = 0, 0 // 初始化
|
||||||
return root.Val
|
dfs(root, 1)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
findLeftValue (root,maxDeep)
|
|
||||||
return value
|
func dfs(root *TreeNode, d int) {
|
||||||
|
if root == nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
func findLeftValue (root *TreeNode,deep int){
|
// 因为先遍历左边,所以左边如果有值,右边的同层不会更新结果
|
||||||
//最左边的值在左边
|
if root.Left == nil && root.Right == nil && depth < d {
|
||||||
if root.Left==nil&&root.Right==nil{
|
depth = d
|
||||||
if deep>maxDeep{
|
res = root.Val
|
||||||
value=root.Val
|
|
||||||
maxDeep=deep
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//递归
|
|
||||||
if root.Left!=nil{
|
|
||||||
deep++
|
|
||||||
findLeftValue(root.Left,deep)
|
|
||||||
deep--//回溯
|
|
||||||
}
|
|
||||||
if root.Right!=nil{
|
|
||||||
deep++
|
|
||||||
findLeftValue(root.Right,deep)
|
|
||||||
deep--//回溯
|
|
||||||
}
|
}
|
||||||
|
dfs(root.Left, d+1) // 隐藏回溯
|
||||||
|
dfs(root.Right, d+1)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -358,14 +349,17 @@ func findLeftValue (root *TreeNode,deep int){
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func findBottomLeftValue(root *TreeNode) int {
|
func findBottomLeftValue(root *TreeNode) int {
|
||||||
queue:=list.New()
|
|
||||||
var gradation int
|
var gradation int
|
||||||
|
queue := list.New()
|
||||||
|
|
||||||
queue.PushBack(root)
|
queue.PushBack(root)
|
||||||
for queue.Len() > 0 {
|
for queue.Len() > 0 {
|
||||||
length := queue.Len()
|
length := queue.Len()
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
node := queue.Remove(queue.Front()).(*TreeNode)
|
node := queue.Remove(queue.Front()).(*TreeNode)
|
||||||
if i==0{gradation=node.Val}
|
if i == 0 {
|
||||||
|
gradation = node.Val
|
||||||
|
}
|
||||||
if node.Left != nil {
|
if node.Left != nil {
|
||||||
queue.PushBack(node.Left)
|
queue.PushBack(node.Left)
|
||||||
}
|
}
|
||||||
@ -394,7 +388,6 @@ var findBottomLeftValue = function(root) {
|
|||||||
maxPath = curPath;
|
maxPath = curPath;
|
||||||
resNode = node.val;
|
resNode = node.val;
|
||||||
}
|
}
|
||||||
// return ;
|
|
||||||
}
|
}
|
||||||
node.left && dfsTree(node.left, curPath+1);
|
node.left && dfsTree(node.left, curPath+1);
|
||||||
node.right && dfsTree(node.right, curPath+1);
|
node.right && dfsTree(node.right, curPath+1);
|
||||||
|
Reference in New Issue
Block a user