update 0513.找树左下角的值 优化 go 和 js 代码

This commit is contained in:
Yuhao Ju
2022-12-03 10:04:19 +08:00
committed by GitHub
parent e8978ea6a8
commit 2b0239d02e

View File

@ -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 findLeftValue (root *TreeNode,deep int){
//最左边的值在左边 func dfs(root *TreeNode, d int) {
if root.Left==nil&&root.Right==nil{ if root == nil {
if deep>maxDeep{ return
value=root.Val
maxDeep=deep
}
}
//递归
if root.Left!=nil{
deep++
findLeftValue(root.Left,deep)
deep--//回溯
} }
if root.Right!=nil{ // 因为先遍历左边,所以左边如果有值,右边的同层不会更新结果
deep++ if root.Left == nil && root.Right == nil && depth < d {
findLeftValue(root.Right,deep) depth = d
deep--//回溯 res = root.Val
} }
dfs(root.Left, d+1) // 隐藏回溯
dfs(root.Right, d+1)
} }
``` ```
@ -358,18 +349,21 @@ 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 {
if node.Left!=nil{ gradation = node.Val
}
if node.Left != nil {
queue.PushBack(node.Left) queue.PushBack(node.Left)
} }
if node.Right!=nil{ if node.Right != nil {
queue.PushBack(node.Right) queue.PushBack(node.Right)
} }
} }
@ -385,19 +379,18 @@ func findBottomLeftValue(root *TreeNode) int {
```javascript ```javascript
var findBottomLeftValue = function(root) { var findBottomLeftValue = function(root) {
//首先考虑递归遍历 前序遍历 找到最大深度的叶子节点即可 //首先考虑递归遍历 前序遍历 找到最大深度的叶子节点即可
let maxPath = 0,resNode = null; let maxPath = 0, resNode = null;
// 1. 确定递归函数的函数参数 // 1. 确定递归函数的函数参数
const dfsTree = function(node,curPath){ const dfsTree = function(node, curPath) {
// 2. 确定递归函数终止条件 // 2. 确定递归函数终止条件
if(node.left===null&&node.right===null){ if(node.left === null && node.right === null) {
if(curPath>maxPath){ if(curPath > maxPath) {
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);
} }
dfsTree(root,1); dfsTree(root,1);
return resNode; return resNode;
@ -409,20 +402,20 @@ var findBottomLeftValue = function(root) {
var findBottomLeftValue = function(root) { var findBottomLeftValue = function(root) {
//考虑层序遍历 记录最后一行的第一个节点 //考虑层序遍历 记录最后一行的第一个节点
let queue = []; let queue = [];
if(root===null){ if(root === null) {
return null; return null;
} }
queue.push(root); queue.push(root);
let resNode; let resNode;
while(queue.length){ while(queue.length) {
let length = queue.length; let length = queue.length;
for(let i=0; i<length; i++){ for(let i = 0; i < length; i++) {
let node = queue.shift(); let node = queue.shift();
if(i===0){ if(i === 0) {
resNode = node.val; resNode = node.val;
} }
node.left&&queue.push(node.left); node.left && queue.push(node.left);
node.right&&queue.push(node.right); node.right && queue.push(node.right);
} }
} }
return resNode; return resNode;