Home>Article>Backend Development> Detailed explanation of steps to obtain binary tree image with PHP

Detailed explanation of steps to obtain binary tree image with PHP

php中世界最好的语言
php中世界最好的语言 Original
2018-05-19 14:49:39 1030browse

This time I will bring you a detailed explanation of the steps to obtain a binary tree image with PHP. What are theprecautionsfor PHP to obtain a binary tree image. The following is a practical case, let's take a look.

Question

Manipulate the given binary tree and transform it into a mirror image of the source binary tree.

Solution idea

There are two ways to flip the binary tree: recursiveand non-recursive. Non-recursive is to usequeue.

Implementation code

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); } }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Lumen timezone How to set the time zone

PHP implementation of merging two sorted linked lists code sharing

The above is the detailed content of Detailed explanation of steps to obtain binary tree image with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn