Leetcode: Binary Search Tree Iterator

本文介绍了如何在不使用递归的情况下,通过迭代方法实现二叉搜索树的中序遍历,从而得到有序的节点序列。通过使用栈来保存待访问节点,确保每次弹出的都是最小的节点,并在此基础上继续访问其右子树,实现高效有序遍历。

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

We essentailly do an in-order traversal (中序遍历) of the tree since the in-order traversal results in a sorted list of node items. 

1 def traverse_binary_tree(node, callback):
2     if node is None:
3         return
4     traverse_binary_tree(node.leftChild, callback)
5     callback(node.value)
6     traverse_binary_tree(node.rightChild, callback)

But here, we should not use recursive (递归) since it will output the ordered list at one time. Alternatively, we interatively (迭代) output the next smallest element with the help of a Stack. In a binary search tree, we always has the value of left node < the value of parent node <= the value of the right node. Therefore, we iteratively visit the nodes on the left path of a given a starting node and push them into the stack. Everytime, we pop the smallest node n, and we need to push all the nodes on the left path starting from the right node of n, namely, n.right.  

Take the following binary search tree as an example. We start from root node 8, we push 8, 3, 1 into the stack. 

If we call hasNext(), we just need to check if the stack is empty or not. 

If we call next(), we pop the top node in the stack, that is node 1. The nodes in the stack is 8, 3.  We further check if node 1 has any right nodes since all its right nodes should be smaller than its parent node 3. Since node 1 does not have right nodes, the next smallest element is node 3. 

Now if we call next(), we pop node 3. The stack contains 8.  Since node 3 has right node, we then push all the nodes on the left path starting from node 6, that are 6 and 4. Now the stack includes 8, 6, 4. We can see we will always keep the smallest node on the top of the stack. 

 

Initially, we push(8), push(3), push(1) --> 8, 3, 1, then for every next() cal, the status of the stack is:

pop(1) --> 8, 3

pop(3), push(6), push(4) --> 8, 6, 4

pop(4) --> 8, 6

pop(6), push(7) --> 8, 7

 

A sample of binary search tree

Here is java code:

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 // we use a Stack to keep the 
11 
12 public class BSTIterator {
13     //Stack<TreeNode> s = new LinkedList<TreeNode>();
14     Deque<TreeNode> s = new ArrayDeque<TreeNode>();
15     public BSTIterator(TreeNode root) {
16         TreeNode n = root;
17         while(n!=null){
18             s.push(n);
19             n = n.left;
20         }
21     }
22 
23     /** @return whether we have a next smallest number */
24     public boolean hasNext() {
25         if(s.isEmpty())
26             return false;
27         return true;
28     }
29 
30     /** @return the next smallest number */
31     public int next() {
32         TreeNode n1 = s.pop();
33         TreeNode l = n1.right;
34         while(l != null){
35             s.push(l);
36             l = l.left;
37         }
38         return n1.val;
39     }
40 }
41 
42 /**
43  * Your BSTIterator will be called like this:
44  * BSTIterator i = new BSTIterator(root);
45  * while (i.hasNext()) v[f()] = i.next();
46  */

 

转载于:https://www.cnblogs.com/kezpitt/p/4300476.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值