this article illustrates the way PHP gets two forked tree images. To share with you for reference, it is as follows:


operates a given two binary tree, and transforms it into the source two fork tree image.

solution idea

flip two fork tree, there are two ways of recursion and non recursion, and non recursion is to use queue.

 < code; PHP /*class TreeNode{var? $val var $left; VAR = NULL; $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-> $node-> left; right; $node- > right = $tmp; if ($node-> left! = NULL) array_push ($queue, $node-> left if ($node->);; right! = NULL) array_push ($queue, $node-> right);}} 

more PHP on the relevant content of interested readers can see the special station: "PHP data structure and algorithm , "php ", summarizes the algorithm program design" php string (string) "PHP array (Array) technique, "PHP commonly used traversal algorithm and skills summary "and" PHP

"math skills summary

hope the PHP program designed to help to everyone.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:A method of obtaining two forked tree images by PHP | Script Home

You may also be interested in these articles!