当前位置:网站首页>Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
2022-08-09 23:14:00 【little black invincible】
Xiaohei realizes after learning
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution:def inorderTraversal(self, root: Optional[TreeNode]span>) -> List[int]:arr = []while root:# pointer to left childpre = root.left# The left child is not empty (the "rightmost" node of the left child is connected to the root, and the root becomes the left child)if pre:# Find the rightmost childwhile pre.right and pre.right != root:pre = pre.right# The rightmost child points to root, then print root, then root is root.rightif pre.right:#pre.right = Nonearr.append(root.val)root = root.right# it is empty, point the right pointer of the rightmost child to root, then root = root.leftelse:pre.right = rootroot = root.left# left child is empty (print, root becomes right child)else:arr.append(root.val)root = root.rightreturn arr
边栏推荐
猜你喜欢
随机推荐
C语言预处理命令是什么?
微软Excel表格点击单元格行和列都显示颜色怎么弄?聚光灯效果设置
PMP每日一练 | 考试不迷路-8.8(包含敏捷+多选)
Usage of placeholder function in Tensorflow
MySQL慢查询的多个原因
Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
How to Make Your Company Content GDPR Compliant
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
蔚来杯2022牛客暑期多校训练营7 CFGJ
Photometric Stereo 光度立体法三维重建
hdu 1503 Advanced Fruits(最长公共子序列的应用)
编程语言中,取余和取模的区别
[corctf 2022] section
fixed investment fund
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
LoRa Basics无线通信技术和应用案例详解
NIO Cup 2022 Nioke Summer Multi-School Training Camp 7 CFGJ
埃氏筛选法:统计素数个数
Excel如何打出正负号?Excel打出正负号的方法
NetCore路由的Endpoint模式