当前位置:网站首页>解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
解决AttributeError: ‘NoneType‘ object has no attribute ‘val‘ if left.val!=right.val:Line 17 问题
2022-08-11 05:29:00 【zhangkai__】
在刷二叉树或者树类型题目时,我经常遇到“AttributeError: 'NoneType' object has no attribute 'val' if left.val!=right.val:Line 17 ” 这样的问题,多做了几道题发现这个问题出现在叶子节点临界值位置
if left is None or right is None:
return False
if left.val!=right.val:
return False当left或right指针已经指向了一个空节点时,left==None,而None类型值就不能继续None.val了,所以这时就会报出AttributeError:属性错误了。
在做二叉树题目时,往往会遇到将空节点继续判断其左右孩子节点的情况,一般这样的思路就出现了问题,递归的条件应该以节点为None就结束了,所以要结合题目情况具体分析,应该在节点为空时,就做出判断,而不是继续判断.val或者.left等。
边栏推荐
猜你喜欢

构建面向特征工程的数据生态 ——拥抱开源生态,OpenMLDB全面打通MLOps生态工具链

The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly

He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone

虚拟机更改IP地址

js 学习进阶(Dom部分 pink老师教学笔记)

栈stack

Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting

Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"

OpenMLDB v0.5.0 发布 | 性能、成本、灵活性再攀高峰

Manufacturer Push Platform-Huawei Access
随机推荐
JS进阶网页特效(pink老师笔记)
Fourth Paradigm OpenMLDB optimization innovation paper was accepted by VLDB, the top international database association
JS案例练习(pink老师经典案例)
The third phase of the contributor task is wonderful
微信小程序云开发项目wx-store代码详解
Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
JVM tuning and finishing
哥德巴赫猜想与整数环
Day 72
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
Jetpack use exception problem collection
黑马大事件项目
经纬度求距离
SearchGuard configuration
谨此留个纪念
【无标题】
Node 踩坑之80端口被占用
Day 82
精彩联动 | OpenMLDB Pulsar Connector原理和实操
JVM调优整理