当前位置:网站首页>解决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等。
边栏推荐
猜你喜欢
![[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow

mk file introduction

USB URB

Day 87

星盟-pwn-fog

经纬度求距离

Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend

stack stack

Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation

The third phase of the contributor task is wonderful
随机推荐
mount命令--挂载出现只读,解决方案
构建面向特征工程的数据生态 ——拥抱开源生态,OpenMLDB全面打通MLOps生态工具链
Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting
Pinyougou project combat notes
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
2021-09-11 C语言 变量与内存分配
Day 73
He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone
Goldbach's conjecture and the ring of integers
stack stack
Day 72
USB in NRZI to encode the data
JNI入门
微信小程序启动页的实现
开源机器学习数据库OpenMLDB贡献者计划全面启动
C语言实现简易扫雷(附带源码)
Day 86
Use c language to implement tic-tac-toe chess (with source code, you can run it directly)
Day 85
The Summer of Open Source 2022 is coming | Welcome to sign up for the OpenMLDB community project~