第七题:交换一棵二叉树中每个节点的左右子树,删除一棵二叉树中的所有叶子节点(递归操作)。

#include<iostream>
#include<cstdlib>
using namespace std;
template<class T>
class BinaryTreeNode{
public:
    T data;
    BinaryTreeNode<T> *leftchild;
    BinaryTreeNode<T> *rightchild;
    BinaryTreeNode(T data = 0){
        this->data = data;
        this->leftchild = rightchild = NULL;
    }
};

template<class T>
class BinaryTree{
private:
    BinaryTreeNode<T> *root;
public:
    BinaryTree(){
        this->root = NULL;
    }
    void visit(BinaryTreeNode<T> *p){
        cout << p->data << "   ";
    }
    BinaryTreeNode<T>* Create(BinaryTreeNode<T> *root);                     //创建二叉树
    void preOrder(BinaryTreeNode<T> *root);                                 //先序遍历二叉树
    void exchange(BinaryTreeNode<T> *root);                                 //交换左右子树
    void deletelea(BinaryTreeNode<T> *root,BinaryTreeNode<T> *pre);         //删除叶子结点
};

template<class T>
BinaryTreeNode<T>* BinaryTree<T> :: Create(BinaryTreeNode<T> *root){
    char x;
    cin >> x;
    if(x == '0')
        return NULL;
    else{
        root = new BinaryTreeNode<T>(x);
        root->leftchild = Create(root->leftchild);
        root->rightchild = Create(root->rightchild);
        return root;
    }
}
template<class T>
void BinaryTree<T> :: preOrder(BinaryTreeNode<T> *root){
    if(root != NULL){
        visit(root);
        preOrder(root->leftchild);
        preOrder(root->rightchild);
    }
}
template<class T>
void BinaryTree<T> :: exchange(BinaryTreeNode<T> *root){
    BinaryTreeNode<T> *temp;
    if(root != NULL){
        temp = root->leftchild;
        root->leftchild = root->rightchild;
        root->rightchild = temp;
        exchange(root->leftchild);
        exchange(root->rightchild);
    }
}
template<class T>
void BinaryTree<T> :: deletelea(BinaryTreeNode<T> *root,BinaryTreeNode<T> *pre){
    if(root!= NULL){
        if(root->leftchild == NULL&&root->rightchild == NULL){
            if(pre != NULL){
                if(root == pre->leftchild)
                    pre->leftchild = NULL;
                if(root == pre->rightchild)
                    pre->rightchild = NULL;
            free(root);
            }
        }
        else{
            pre = root;
            deletelea(root->leftchild,pre);
            deletelea(root->rightchild,pre);
        }

    }
}
int main(){
    BinaryTreeNode<char> *root;
    BinaryTreeNode<char> *pre = NULL;
    BinaryTree<char> *t;
    root = t->Create(root);
    t->preOrder(root);
    /*cout << endl << "交换后的二叉树先序序列为:" << endl;
    t->exchange(root);
    t->preOrder(root);*/
    cout << endl << "删除叶子结点后的二叉树先序序列为:" << endl;
    t->deletelea(root,pre);
    t->preOrder(root);
return 0;
}

 

### 回答1: 好的,我来回复。 这个任务要求我们使用递归的方式实现二叉树左右子树交换,并输出后序遍历结果。 具体实现方法可以使用栈来辅助实现。首先将根节点入栈,然后循环执行以下操作: 1. 取出栈顶节点,如果该节点存在左右子树,则交换左右子树,并将左右子树入栈。 2. 如果该节点没有左右子树,则将该节点的值输出,并将该节点出栈。 3. 重复执行以上两步,直到栈为空。 最后输出的结果就是二叉树交换后的后序遍历结果。 希望我的回复能够帮到您。 ### 回答2: 二叉树左右子树交换是一道经典的二叉树操作,非递归实现可以通过使用栈来实现。具体实现过程如下: 首先,将根节点入栈,然后进入循环,栈不为空时执行以下步骤: 1.从栈弹出一个节点,将其左右子树交换。 2.如果该节点存在左子树,则将其左子树入栈;如果存在右子树,则将其右子树入栈。 3.重复执行1和2步,直到栈为空。 最后,进行后序遍历输出即可。 代码实现: ```python def invertTree(root): if not root: return None stack = [root] while stack: node = stack.pop() node.left, node.right = node.right, node.left if node.left: stack.append(node.left) if node.right: stack.append(node.right) result = [] def postorder(root): if not root: return postorder(root.left) postorder(root.right) result.append(root.val) postorder(root) return result ``` 以上代码实现了二叉树左右子树交换,并输出后序遍历结果。时间复杂度为O(n),因为需要遍历所有节点,空间复杂度为O(n),因为使用了一个栈来存储所有节点。 ### 回答3: 二叉树左右子树交换的算法可以使用迭代的方式实现。主要思路是利用栈结构进行遍历,遍历到一个节点时,将其左右节点互换,然后将其左右节点入栈,再继续遍历。直到栈为空时,遍历结束。此时,可以利用栈结构对二叉树进行后序遍历。 具体实现: 1. 首先将根节点入栈。 2. 进入循环,将栈顶的节点出栈,将其左右节点交换,然后将左右节点入栈。 3. 判断栈是否为空,如果是,则退出循环。 4. 否则,继续循环,取出栈顶的节点,并对其进行交换和入栈操作。 5. 当遍历结束后,可以利用栈结构对二叉树进行后序遍历,具体方法是将每个出栈的节点放入另一个栈结构,最后依次出栈即可。 代码实现: ```python def invertTree(root: TreeNode) -> TreeNode: if not root: return root stack = [root] while stack: node = stack.pop() node.left, node.right = node.right, node.left if node.left: stack.append(node.left) if node.right: stack.append(node.right) # 后序遍历输出结果 postorder = [] stack = [root] while stack: node = stack.pop() postorder.append(node.val) if node.left: stack.append(node.left) if node.right: stack.append(node.right) return postorder[::-1] ``` 以上是一种非递归方式的二叉树左右子树交换和后序遍历的实现方法
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值