当前位置:网站首页>解决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等。
边栏推荐
- 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
- Tinker的自我介绍
- Day 80
- JS案例练习(pink老师经典案例)
- js 学习进阶(Dom部分 pink老师教学笔记)
- IIC and SPI
- Byte (byte) and bit (bit)
- 实时特征计算平台架构方法论和基于 OpenMLDB 的实践
- 127.0.0.1 已拒绝连接
- [Meetup]OpenMLDBxDolphinScheduler 链接特征工程与调度环节,打造端到端MLOps工作流
猜你喜欢

stack stack

活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光

Intelligent risk control China design and fall to the ground

js 学习进阶(事件高级 pink老师教学笔记)

何凯明新作ViTDET:目标检测领域,颠覆分层backbone理念

gerrit configure SSH Key and account, email information

论文解读TransFG: A Transformer Architecture for Fine-grained Recognition

Node stepping on the pit 80 port is occupied

星盟-pwn-babyheap

Jetpack之dataBinding
随机推荐
栈stack
mongoose连接mongodb不错,显示encoding没有定义
Js method commonly used objects and attributes
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights
Day 86
IIC 和 SPI
微信小程序启动页的实现
系统性能及并发数的一些计算公式
mysql基础总结
Day 85
127.0.0.1 已拒绝连接
SearchGuard配置
OpenMLDB + Jupyter Notebook: Quickly Build Machine Learning Applications
C语言实现简易扫雷(附带源码)
Day 67
OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
Day 78
Promise.race learning (judging the fastest execution of multiple promise objects)
SearchGuard证书配置
vim 编辑器使用学习