时间:2021-07-01 10:21:17 帮助过:19人阅读
问题
操作给定的二叉树,将其变换为源二叉树的镜像。
解决思路
翻转二叉树,有递归和非递归两种方式,非递归就是使用队列。
实现代码
<?php
/*class TreeNode{
var $val;
var $left = NULL;
var $right = NULL;
function construct($val){
$this->val = $val;
}
}*/
function Mirror(&$root)
{
if($root == NULL)
return 0;
$queue = array();
array_push($queue, $root);
while(!empty($queue)){
$node = array_shift($queue);
$tmp = $node->left;
$node->left = $node->right;
$node->right = $tmp;
if($node->left != NULL)
array_push($queue, $node->left);
if($node->right != NULL)
array_push($queue, $node->right);
}
}相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
Lumen timezone 怎样进行时区设置
PHP实现合并两个排序链表代码分享
以上就是PHP获取二叉树镜像步骤详解的详细内容,更多请关注Gxl网其它相关文章!