mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
update 0404.左叶子之和: 修改 go 代码,优化 js 代码风格
This commit is contained in:
@ -35,7 +35,7 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
相信通过这两个图,大家可以最左叶子的定义有明确理解了。
|
相信通过这两个图,大家对最左叶子的定义有明确理解了。
|
||||||
|
|
||||||
那么**判断当前节点是不是左叶子是无法判断的,必须要通过节点的父节点来判断其左孩子是不是左叶子。**
|
那么**判断当前节点是不是左叶子是无法判断的,必须要通过节点的父节点来判断其左孩子是不是左叶子。**
|
||||||
|
|
||||||
@ -298,48 +298,49 @@ class Solution:
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func sumOfLeftLeaves(root *TreeNode) int {
|
func sumOfLeftLeaves(root *TreeNode) int {
|
||||||
var res int
|
if root == nil {
|
||||||
findLeft(root,&res)
|
return 0
|
||||||
return res
|
|
||||||
}
|
|
||||||
func findLeft(root *TreeNode,res *int){
|
|
||||||
//左节点
|
|
||||||
if root.Left!=nil&&root.Left.Left==nil&&root.Left.Right==nil{
|
|
||||||
*res=*res+root.Left.Val
|
|
||||||
}
|
}
|
||||||
if root.Left!=nil{
|
leftValue := sumOfLeftLeaves(root.Left) // 左
|
||||||
findLeft(root.Left,res)
|
|
||||||
}
|
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
|
||||||
if root.Right!=nil{
|
leftValue = root.Left.Val // 中
|
||||||
findLeft(root.Right,res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rightValue := sumOfLeftLeaves(root.Right) // 右
|
||||||
|
|
||||||
|
return leftValue + rightValue
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**迭代法**
|
**迭代法(前序遍历)**
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func sumOfLeftLeaves(root *TreeNode) int {
|
func sumOfLeftLeaves(root *TreeNode) int {
|
||||||
var res int
|
st := make([]*TreeNode, 0)
|
||||||
queue:=list.New()
|
if root == nil {
|
||||||
queue.PushBack(root)
|
return 0
|
||||||
for queue.Len()>0{
|
}
|
||||||
length:=queue.Len()
|
st = append(st, root)
|
||||||
for i:=0;i<length;i++{
|
result := 0
|
||||||
node:=queue.Remove(queue.Front()).(*TreeNode)
|
|
||||||
if node.Left!=nil&&node.Left.Left==nil&&node.Left.Right==nil{
|
for len(st) != 0 {
|
||||||
res=res+node.Left.Val
|
node := st[len(st)-1]
|
||||||
|
st = st[:len(st)-1]
|
||||||
|
if node.Left != nil && node.Left.Left == nil && node.Left.Right == nil {
|
||||||
|
result += node.Left.Val
|
||||||
}
|
}
|
||||||
if node.Left!=nil{
|
if node.Right != nil {
|
||||||
queue.PushBack(node.Left)
|
st = append(st, node.Right)
|
||||||
}
|
}
|
||||||
if node.Right!=nil{
|
if node.Left != nil {
|
||||||
queue.PushBack(node.Right)
|
st = append(st, node.Left)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return res
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@ -351,16 +352,16 @@ func sumOfLeftLeaves(root *TreeNode) int {
|
|||||||
var sumOfLeftLeaves = function(root) {
|
var sumOfLeftLeaves = function(root) {
|
||||||
//采用后序遍历 递归遍历
|
//采用后序遍历 递归遍历
|
||||||
// 1. 确定递归函数参数
|
// 1. 确定递归函数参数
|
||||||
const nodesSum = function(node){
|
const nodesSum = function(node) {
|
||||||
// 2. 确定终止条件
|
// 2. 确定终止条件
|
||||||
if(node===null){
|
if(node === null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
let leftValue = nodesSum(node.left);
|
let leftValue = nodesSum(node.left);
|
||||||
let rightValue = nodesSum(node.right);
|
let rightValue = nodesSum(node.right);
|
||||||
// 3. 单层递归逻辑
|
// 3. 单层递归逻辑
|
||||||
let midValue = 0;
|
let midValue = 0;
|
||||||
if(node.left&&node.left.left===null&&node.left.right===null){
|
if(node.left && node.left.left === null && node.left.right === null) {
|
||||||
midValue = node.left.val;
|
midValue = node.left.val;
|
||||||
}
|
}
|
||||||
let sum = midValue + leftValue + rightValue;
|
let sum = midValue + leftValue + rightValue;
|
||||||
@ -374,19 +375,19 @@ var sumOfLeftLeaves = function(root) {
|
|||||||
```javascript
|
```javascript
|
||||||
var sumOfLeftLeaves = function(root) {
|
var sumOfLeftLeaves = function(root) {
|
||||||
//采用层序遍历
|
//采用层序遍历
|
||||||
if(root===null){
|
if(root === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
let queue = [];
|
let queue = [];
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
queue.push(root);
|
queue.push(root);
|
||||||
while(queue.length){
|
while(queue.length) {
|
||||||
let node = queue.shift();
|
let node = queue.shift();
|
||||||
if(node.left!==null&&node.left.left===null&&node.left.right===null){
|
if(node.left !== null && node.left.left === null && node.left.right === null) {
|
||||||
sum+=node.left.val;
|
sum+=node.left.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 sum;
|
return sum;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user