二叉树的前序遍历以及Copy树

本文介绍了一种使用C++实现二叉树的方法,包括通过递归方式创建二叉树及进行前序遍历的过程,并尝试复制一棵二叉树。

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

#include<iostream>
using namespace std;
template<class DataType>
struct BiNode
{
    DataType data;
    BiNode<DataType> *rchild,*lchild;
};
template<class DataType>
class BiTree
{
private:
public:

    BiNode<DataType> *Creat(BiNode<DataType> *bt)
    {
        char ch;
        cin>>ch;//输入节点数据
        if(ch=='#')bt=NULL;//建立一颗空树
        else
        {
            bt=new BiNode<DataType>;
            bt->data=ch;//生成一个节点,数据域为ch
            bt->lchild=Creat(bt->lchild);//递归建立左子树
            bt->rchild=Creat(bt->rchild);//递归建立右子树
        }
        return bt;
    }

    void PreOrder(BiNode<DataType> *bt)//前序遍历
    {
        if(bt==NULL)return;//递归调用的结束条件
        else
        {
            cout<<bt->data<<endl;//前序递归遍历bt的数据域
            PreOrder(bt->lchild);//前序递归遍历bt的左子树
            PreOrder(bt->rchild);//前序递归遍历bt的左子树
        }
    }

    void Copy(BiNode<DataType> *at,BiNode<DataType> *bt)
    {
       if(bt==NULL)return;//递归调用的结束条件
        else
        {
            at=new BiNode<DataType>;
            at->data=bt->data;
            cout<<at->data<<endl;//前序递归遍历bt的数据域
            Copy(at->lchild,bt->lchild);//前序递归遍历bt的左子树
            Copy(at->rchild,bt->rchild);//前序递归遍历bt的左子树
        }
    }
};

int main()
{
    BiNode<char> *root,*root1;
    BiTree<char> b;
    root = b.Creat(root);
    cout<<endl;
    b.PreOrder(root);
    b.Copy(root,root1);
    return 0;
}


1. 创建二叉树 二叉树是一种形结构,每个节点最多有两个子节点。我们可以通过递归的方式来创建二叉树。假设我们有一个数组arr表示二叉树的结构,-1表示空节点,其他数字表示节点的值。我们可以采用如下方式来创建二叉树: ```python class Node: def __init__(self, val): self.val = val self.left = None self.right = None def create_tree(arr, index): if index >= len(arr) or arr[index] == -1: return None root = Node(arr[index]) root.left = create_tree(arr, 2*index+1) root.right = create_tree(arr, 2*index+2) return root ``` 其中create_tree函数是递归创建二叉树的函数,参数arr表示数组,index表示当前节点在数组中的下标。如果当前节点为-1或者下标超出数组范围,则返回None,否则就创建一个节点,并将左子和右子递归创建。 2. 求二叉树前序遍历序列 前序遍历是指先遍历根节点,然后遍历左子,最后遍历右子。我们可以采用递归的方式来实现前序遍历。 ```python def preorder_traversal(root): if not root: return [] res = [root.val] res += preorder_traversal(root.left) res += preorder_traversal(root.right) return res ``` 其中preorder_traversal函数是递归实现前序遍历的函数,参数root表示根节点。如果根节点为空,则返回空列表。否则就先将根节点的值加入结果列表,然后递归遍历左子和右子,并将结果合并。 3. 求二叉树的中序遍历序列 中序遍历是指先遍历左子,然后遍历根节点,最后遍历右子。我们可以采用递归的方式来实现中序遍历。 ```python def inorder_traversal(root): if not root: return [] res = inorder_traversal(root.left) res += [root.val] res += inorder_traversal(root.right) return res ``` 其中inorder_traversal函数是递归实现中序遍历的函数,参数root表示根节点。如果根节点为空,则返回空列表。否则就先递归遍历左子,然后将根节点的值加入结果列表,最后递归遍历右子,并将结果合并。 4. 求二叉树的后序遍历序列 后序遍历是指先遍历左子,然后遍历右子,最后遍历根节点。我们可以采用递归的方式来实现后序遍历。 ```python def postorder_traversal(root): if not root: return [] res = postorder_traversal(root.left) res += postorder_traversal(root.right) res += [root.val] return res ``` 其中postorder_traversal函数是递归实现后序遍历的函数,参数root表示根节点。如果根节点为空,则返回空列表。否则就先递归遍历左子和右子,然后将根节点的值加入结果列表,并将结果合并。 5. 求二叉树的层次遍历序列 层次遍历是指按照的层次遍历节点。我们可以采用队列来实现二叉树的层次遍历。 ```python def level_order_traversal(root): if not root: return [] res = [] queue = [root] while queue: cur_level = [] for i in range(len(queue)): node = queue.pop(0) cur_level.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) res.append(cur_level) return res ``` 其中level_order_traversal函数是实现二叉树层次遍历的函数,参数root表示根节点。如果根节点为空,则返回空列表。否则就先将根节点加入队列,然后依次将队列中的节点取出来,并将它们的左子和右子加入队列,直到队列为空。 6. 计算二叉树的深度 二叉树的深度是指从根节点到叶子节点的最长路径长度。我们可以采用递归的方式来计算二叉树的深度。 ```python def tree_depth(root): if not root: return 0 left_depth = tree_depth(root.left) right_depth = tree_depth(root.right) return max(left_depth, right_depth) + 1 ``` 其中tree_depth函数是递归实现计算二叉树深度的函数,参数root表示根节点。如果根节点为空,则返回0。否则就递归计算左子和右子的深度,并取最大值加1。 7. 统计二叉树中叶子结点的个数 叶子节点是指没有子节点的节点。我们可以采用递归的方式来统计二叉树中叶子节点的个数。 ```python def count_leaf_node(root): if not root: return 0 if not root.left and not root.right: return 1 return count_leaf_node(root.left) + count_leaf_node(root.right) ``` 其中count_leaf_node函数是递归实现统计叶子节点个数的函数,参数root表示根节点。如果根节点为空,则返回0。否则就判断当前节点是否为叶子节点,如果是则返回1,否则递归计算左子和右子的叶子节点个数,并将它们相加。 8. 判断两棵是否相等 判断两棵是否相等,需要对两棵进行遍历,并比较每个节点的值是否相等。我们可以采用递归的方式来判断两棵是否相等。 ```python def is_same_tree(p, q): if not p and not q: return True if not p or not q: return False if p.val != q.val: return False return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right) ``` 其中is_same_tree函数是递归实现判断两棵是否相等的函数,参数p和q分别表示两棵的根节点。如果两棵的根节点都为空,则返回True;如果其中有一棵的根节点为空,则返回False;否则就比较两棵的根节点的值是否相等,然后递归比较左子和右子。 9. 交换二叉树中每个结点的左孩子和右孩子 交换二叉树中每个节点的左孩子和右孩子,可以采用递归的方式来实现。 ```python def invert_tree(root): if not root: return None root.left, root.right = root.right, root.left invert_tree(root.left) invert_tree(root.right) return root ``` 其中invert_tree函数是递归实现交换二叉树中每个节点的左孩子和右孩子的函数,参数root表示根节点。如果根节点为空,则返回None;否则就先交换根节点的左孩子和右孩子,然后递归交换左子和右子。 10. 复制二叉树 复制一棵二叉树,可以采用递归的方式来实现。 ```python def copy_tree(root): if not root: return None new_root = Node(root.val) new_root.left = copy_tree(root.left) new_root.right = copy_tree(root.right) return new_root ``` 其中copy_tree函数是递归实现复制二叉树的函数,参数root表示根节点。如果根节点为空,则返回None;否则就先创建一个新节点并赋值为原节点的值,然后递归复制左子和右子,并将它们赋值给新节点的左孩子和右孩子。最后返回新根节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值