leetcode-173-Binary Search Tree Iterator

本文探讨了在二叉搜索树(BST)中使用迭代方式实现中序遍历的方法,考虑到内存限制,避免了递归带来的O(n)内存占用。通过创建栈、向左子树推进并打印节点,最后向右子树移动的步骤,实现了高效的BST中序遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Error:
do not how to do: actually it use iterate inorder for a tree, since inorder for BST is the sorted array of the tree. Also, since we only allow to use O(h) memory, we cannot recursive inorder and store it use array/vector, since it has O(n) memory. So we need to iterate the BST, it has three step:
1. create a stack
2. push current node and move cur = cur->left until it null
3. print the top of stack, and move cur = cur->right
4. repeat it until stack is empty

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值