173. 二叉搜索树迭代器(中序遍历的递归法与迭代法)

173. 二叉搜索树迭代器(中序遍历的递归法与迭代法)

实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器:
BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。
boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true ;否则返回 false 。
int next()将指针向右移动,然后返回指针处的数字。
注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。

你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。

递归法

class BSTIterator {
    private List<Integer> res;
    private int index = 0; 

    public BSTIterator(TreeNode root) {
        res = new ArrayList<>();
        inOrder(root); 
        
    }
    public void inOrder(TreeNode root)
    { 
    	//中序递归的常规做法
        if(root == null) return ;
        inOrder(root.left);
        res.add(root.val);
        inOrder(root.right);
    }
    
    public int next() {
        return res.get(index++);
    }
    
    public boolean hasNext() {
        if((index) == res.size()) return false;
        return true;
    }
}

非递归迭代法

首先有迭代法的模板

//二叉树的先根遍历
void preOrder(BTNode* root) {
    stack<BTNode*> st;
    while (root or not st.empty()) {
        while (root) {
            cout << root->val << " ";
            st.push(root);
            root = root->left;
        }
        root = st.top();
        st.pop();
        root = root->right;
    }
}
//二叉树的中根遍历
//注意中根遍历和先根遍历代码的差别仅仅在于ans.push_back(root->val);的位置
void inOrder(BTNode* root) {
    stack<BTNode*> st;
    while (root or not st.empty()) {
        while (root) {   //步骤1
            st.push(root);
            root = root->left;
        }
        root = st.top();    //步骤2
        st.pop();
        cout << root->val << " ";
        root = root->right; //步骤3
    }
}
//二叉树的后根遍历
//注意后根遍历和先根遍历代码的差别在于先将右子树入栈,再将左子树入栈,最后进行一次翻转
void postOrder(BTNode* root) {
    vector<gg> ans;
    stack<BTNode*> st;
    while (root or not st.empty()) {
        while (root) {
            ans.push_back(root->val);
            st.push(root);
            root = root->right;  
        }
        root = st.top();
        st.pop();
        root = root->left;  
    }
    reverse(begin(ans), end(ans));
    for(gg i: ans){
        cout << i << " ";
    }
}

从中可以看出迭代法是按3步走的,1.遍历左子树 2.根 3.遍历右子树

那么本题有两种做法,

1.直接迭代出答案然后在next的函数遍历

2.利用一个队列在next的函数中处理树

class BSTIterator {
    Deque<TreeNode> d = new ArrayDeque<>();
    public BSTIterator(TreeNode root) {
        // 步骤 1
        dfsLeft(root);
    }
    
    public int next() {
        // 步骤 2
        TreeNode root = d.pollLast();
        int ans = root.val;
        // 步骤 3
        root = root.right;
        // 步骤 1
        dfsLeft(root);
        return ans;
    }

    void dfsLeft(TreeNode root) {
        while (root != null) {
            d.addLast(root);
            root = root.left;
        }
    }
    
    public boolean hasNext() {
        return !d.isEmpty();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言实现用二叉链表作为存储结构,输入键值序列建立一棵二叉排序树,然后中序遍历这棵二叉树的代码: ```c #include <stdio.h> #include <stdlib.h> //定义二叉树结构体 typedef struct TreeNode { int value; struct TreeNode *left; struct TreeNode *right; } TreeNode; //插入节点 TreeNode* insert(TreeNode *root, int value) { if (root == NULL) { TreeNode *newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->value = value; newNode->left = NULL; newNode->right = NULL; return newNode; } if (value < root->value) { root->left = insert(root->left, value); } else if (value > root->value) { root->right = insert(root->right, value); } return root; } //中序遍历 void inorderTraversal(TreeNode *root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->value); inorderTraversal(root->right); } int main() { TreeNode *root = NULL; int n, value; printf("请输入节点数:"); scanf("%d", &n); printf("请输入节点值:"); for (int i = 0; i < n; i++) { scanf("%d", &value); root = insert(root, value); } printf("中序遍历结果:"); inorderTraversal(root); printf("\n"); return 0; } ``` 代码的思路是: 1. 定义二叉树结构体,包括节点值、左子树指针、右子树指针。 2. 插入节点函数实现二叉排序树的构建,如果节点为空则新建节点,否则递归查找左右子树进行插入。 3. 中序遍历函数实现输出节点值的操作。 4. 主函数中输入节点数和节点值,构建二叉排序树,并输出中序遍历的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值