当前位置:网站首页>解决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生态工具链
- Thesis unscramble TransFG: A Transformer Architecture for Fine - grained Recognition
- Node 踩坑之80端口被占用
- gerrit configure SSH Key and account, email information
- 场景驱动的特征计算方式OpenMLDB,高效实现“现算先用”
- Day 67
- 栈stack
- JS case exercise (classic case of teacher pink)
- Tinker的自我介绍
- Here is a memorial
猜你喜欢
OpenMLDB: Consistent production-level feature computing platform online and offline
JS事件循环机制
Manufacturer Push Platform-Huawei Access
星盟-pwn-babyfmt
将一个excel文件中多个sheet页“拆分“成多个“独立“excel文件
127.0.0.1 已拒绝连接
Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
USB 枚举过程中8 字节标准请求解析
js learning advanced BOM part (pink teacher notes)
经纬度求距离
随机推荐
2021-09-11 C language variables and memory allocation
The whole process of Tinker access --- configuration
场景驱动的特征计算方式OpenMLDB,高效实现“现算先用”
JS进阶网页特效(pink老师笔记)
Jetpack之dataBinding
Day 73
ARM 汇编指令 ADR 与 LDR 使用
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~
vim 编辑器使用学习
USB in NRZI to encode the data
mount命令--挂载出现只读,解决方案
Getting Started with JNI
Tinker's self-introduction
Day 85
使用c语言实现井字棋(有源码,可以直接运行)
Asis2016 books null off by one
SearchGuard configuration
将一个excel文件中多个sheet页“拆分“成多个“独立“excel文件
虚拟机更改IP地址
OpenMLDB Meetup No.2 会议纪要