php怎么重建二叉树代码

worktile 其他 129

回复

共3条回复 我来回复
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    PHP重建二叉树的代码可以按照以下步骤实现:

    1. 创建一个名为TreeNode的类,用于表示二叉树的节点。
    “`php
    class TreeNode {
    public $val;
    public $left;
    public $right;

    public function __construct($val) {
    $this->val = $val;
    $this->left = null;
    $this->right = null;
    }
    }
    “`

    2. 创建一个名为buildTree的函数,用于重建二叉树。
    “`php
    function buildTree($preorder, $inorder) {
    // 判断前序遍历和中序遍历数组是否为空
    if (empty($preorder) || empty($inorder)) {
    return null;
    }

    // 取出前序遍历数组的第一个元素作为根节点的值
    $rootValue = array_shift($preorder);

    // 创建根节点
    $root = new TreeNode($rootValue);

    // 查找根节点在中序遍历数组中的位置
    $rootIndex = array_search($rootValue, $inorder);

    // 递归构建左子树
    $root->left = buildTree(array_slice($preorder, 0, $rootIndex), array_slice($inorder, 0, $rootIndex));

    // 递归构建右子树
    $root->right = buildTree(array_slice($preorder, $rootIndex), array_slice($inorder, $rootIndex + 1));

    return $root;
    }
    “`

    3. 调用buildTree函数进行测试。
    “`php
    $preorder = [1, 2, 4, 5, 3, 6, 7];
    $inorder = [4, 2, 5, 1, 6, 3, 7];

    $root = buildTree($preorder, $inorder);

    // 输出重建的二叉树的前序遍历结果
    function preorderTraversal($root) {
    if ($root == null) {
    return;
    }

    echo $root->val . ” “;
    preorderTraversal($root->left);
    preorderTraversal($root->right);
    }

    preorderTraversal($root); // 输出:1 2 4 5 3 6 7
    “`

    以上代码实现了用PHP重建二叉树的过程。通过给定的前序遍历和中序遍历数组,可以成功重建一棵二叉树,并输出其前序遍历结果。

    2年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    重建二叉树是指根据二叉树的前序遍历序列和中序遍历序列,构建出原始的二叉树结构。在PHP中,可以通过递归的方式来实现二叉树的重建。下面是一个示例代码:

    “`php
    class TreeNode {
    public $val;
    public $left = null;
    public $right = null;

    public function __construct($val) {
    $this->val = $val;
    }
    }

    function buildTree($preorder, $inorder) {
    if (empty($preorder) || empty($inorder)) {
    return null;
    }

    // 取出前序遍历序列的第一个元素作为根节点
    $rootVal = $preorder[0];
    $root = new TreeNode($rootVal);

    // 在中序遍历序列中找到根节点的位置
    $rootIndex = array_search($rootVal, $inorder);

    // 划分左子树的前序遍历序列、中序遍历序列
    $leftPreorder = array_slice($preorder, 1, $rootIndex);
    $leftInorder = array_slice($inorder, 0, $rootIndex);

    // 划分右子树的前序遍历序列、中序遍历序列
    $rightPreorder = array_slice($preorder, $rootIndex + 1);
    $rightInorder = array_slice($inorder, $rootIndex + 1);

    // 递归构建左子树和右子树
    $root->left = buildTree($leftPreorder, $leftInorder);
    $root->right = buildTree($rightPreorder, $rightInorder);

    return $root;
    }

    // 示例数据
    $preorder = [1, 2, 4, 7, 3, 5, 6, 8];
    $inorder = [4, 7, 2, 1, 5, 3, 8, 6];

    // 调用函数重建二叉树
    $root = buildTree($preorder, $inorder);

    // 打印重建后的二叉树
    function printTree($root) {
    if ($root == null) {
    return;
    }

    echo $root->val . ” “;
    printTree($root->left);
    printTree($root->right);
    }

    printTree($root);
    “`

    在以上示例代码中,我们首先定义了一个`TreeNode`类表示二叉树节点,并实现了`buildTree()`函数用于重建二叉树。该函数接收两个参数,分别是前序遍历序列和中序遍历序列。然后递归地进行重建,每次递归都将前序遍历序列和中序遍历序列划分为左子树和右子树的部分,并创建节点。最后打印重建后的二叉树。

    值得注意的是,在PHP中,数组下标从0开始,所以在划分左子树和右子树的前序遍历序列和中序遍历序列时,对于左子树的部分,我们需要使用`array_slice()`函数切割数组,并指定起始位置和长度。

    2年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    重建二叉树是指根据给定的二叉树前序遍历和中序遍历的结果,构建出原来的二叉树。

    实现重建二叉树的方法有很多,其中一种常用的方法是使用递归算法。

    下面是使用PHP代码实现重建二叉树的步骤:

    Step 1: 创建节点类

    首先我们需要创建一个节点类来表示二叉树的节点,节点类包括节点的值(data)和左右子节点(left和right)。

    “`php
    class TreeNode {
    public $data;
    public $left;
    public $right;

    public function __construct($data) {
    $this->data = $data;
    $this->left = null;
    $this->right = null;
    }
    }
    “`

    Step 2: 实现重建二叉树函数

    接下来,我们需要实现一个函数来重建二叉树。该函数接收两个参数,分别是前序遍历数组和中序遍历数组。

    “`php
    function rebuildBinaryTree($preorder, $inorder) {
    // 判断数组是否为空
    if (empty($preorder) || empty($inorder)) {
    return null;
    }

    // 根据前序遍历数组的第一个元素创建根节点
    $rootValue = $preorder[0];
    $root = new TreeNode($rootValue);

    // 在中序遍历数组中查找根节点的索引
    $rootIndex = array_search($rootValue, $inorder);

    // 递归构建左子树
    $root->left = rebuildBinaryTree(array_slice($preorder, 1, $rootIndex), array_slice($inorder, 0, $rootIndex));

    // 递归构建右子树
    $root->right = rebuildBinaryTree(array_slice($preorder, $rootIndex + 1), array_slice($inorder, $rootIndex + 1));

    return $root;
    }
    “`

    Step 3: 测试重建二叉树函数

    下面是一个示例,展示如何使用重建二叉树函数重建一个二叉树:

    “`php
    $preorder = [1, 2, 4, 7, 3, 5, 6, 8];
    $inorder = [4, 7, 2, 1, 5, 3, 8, 6];

    $root = rebuildBinaryTree($preorder, $inorder);

    // 遍历重建后的二叉树
    function inorderTraversal($root) {
    if ($root == null) {
    return;
    }

    inorderTraversal($root->left);
    echo $root->data . ” “;
    inorderTraversal($root->right);
    }

    inorderTraversal($root); // 输出: 4 7 2 1 5 3 8 6
    “`

    以上就是使用PHP代码实现重建二叉树的方法。通过递归算法,我们根据给定的前序遍历和中序遍历,成功重建出原来的二叉树。

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部