【问题标题】:Invert Binary Tree in PHP在 PHP 中反转二叉树
【发布时间】:2019-01-29 03:25:38
【问题描述】:

伙计们,我在 PHP 中遇到了这个问题,我正在尝试在 PHP 中反转二叉树,但我不知道如何解决这个问题。

任务是倒置二叉树,所以叶子的顺序是倒置的。

例子:

    1
   / \
  2   3
 / \ / \
4  5 6  7

转换为:

    1
   / \
  3   2
 / \ / \
7  6 5  4

注意:请记住,树也可能不平衡。

/**
 * leaf data structure
 */
class BinaryNode {

    /** @var mixed null */
    public $value = null;
    /** @var BinaryNode null */
    public $left = null;
    /** @var BinaryNode null */
    public $right = null;

    /**
     * @param mixed $value
     */
    public function __construct( $value ) {
        $this->value = $value;
    }
}

class BinaryTree
{
    /**
     * @param BinaryNode $root
     * @return BinaryNode
     */
    public static function invert($root): BinaryNode
    {
        //$BinaryNode = new BinaryNode();

        if(!isset($root)) return $root;

        $tempLeftNode = $root->left;

        $root->left = $root->right;
        $root->right = $tempLeftNode;

        self::invert($root->left);
        self::invert($root->right);

        return  $root;

    }
}

$root = new BinaryNode(1);

$root->left = new BinaryNode(2);
$root->right = new BinaryNode(3);

$root->left->left = new BinaryNode(4);
$root->left->right = new BinaryNode(5);

$root->right->left = new BinaryNode(6);
$root->right->right = new BinaryNode(7);

print_r(BinaryTree::invert($root));

【问题讨论】:

  • 你的 BinaryTree 类有一个 invert 方法。它不能正常工作吗?它有什么作用?
  • 欢迎来到 SO 和好问题。请注意,您的 invert 函数在叶子上返回 null ,但您的返回类型不允许这样做,因此请考虑修复它以进行编译 - 如果您删除返回类型说明符,您的代码会产生您想要的输出。
  • 欢迎来到 SO 并请测试答案。与您的问题互动。 :)
  • 谢谢你们,我删除了 invert 调用,它现在可以正常工作了

标签: php tree binary


【解决方案1】:

你可以使用递归函数来做到这一点......我记得几年前做过这样的练习......好吧,我的解决方案是这样的:

$array = [
    'a' => [
        'b1' => [
            'c1' => [
                'e1' => 4,
                'f1' => 5,
                'g1' => 6,
            ],
            'd1' => [
                'e11' => 4,
                'f11' => 5,
                'g11' => 6,
            ]
        ],
        'b2' => [
            'c2' => [
                'e2' => 4,
                'f2' => 5,
                'g2' => 6,
            ],
            'd2' => [
                'e21' => 4,
                'f21' => 5,
                'g21' => 6,
            ]
        ],
    ]
];

附带功能:

function reverse_recursively($arrayInput) {
    foreach ($arrayInput as $key => $input) {
        if (is_array($input)) {
            $arrayInput[$key] = reverse_recursively($input);
        }
    }

    return array_reverse($arrayInput);
}

echo '<pre>';
print_r($array);
echo '<br>';
print_r(reverse_recursively($array));

你可以在这里看到测试:https://3v4l.org/2pYhR

【讨论】:

  • 谢谢你,Rafael,分享,我解决了这个问题,感谢你的时间
  • 如果您确实使用了此答案,请投票并将问题标记为已回答。 :)
【解决方案2】:
function invertTree($root) {

    if($root == null)
        return null;
    $flag = $root->right;
    $root->right = invertTree($root->left);
    $root->left = invertTree($flag);
    return $root;
}

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    相关资源
    最近更新 更多