Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize Mirror binary Tree with PHP

Shulou Source: shulou.com Published: 2022-06-02 04:53:58 09月16日 Update

This article mainly introduces "how to achieve mirror binary tree in PHP". In daily operation, I believe many people have doubts about how to achieve mirror binary tree in PHP. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve mirror binary tree in PHP". Next, please follow the editor to study!

Manipulate the given binary tree and transform it into a mirror image of the source binary tree. Image definition of binary tree: source binary tree /\ 10 /\ /\ 7 9 11 Image binary tree /\ 6 /\ /\ 9 7 5 idea: 1. The left subtree is assigned to temp2.temp and the right subtree 3. The right subtree is assigned to the left subtree. Recursive mirror (root) temp=root- > left root- > left=root- > right root-right=temp mirror (root- > left) mirror (root- > right) class TreeNode {var $val; var $left= NULL; var $right= NULL; function _ construct ($val) {$this- > val = $val;} function Mirror (& $root) {if ($root==null) {return null;} $temp=$root- > left $root- > left=$root- > right; $root- > right=$temp; Mirror ($root- > left); Mirror ($root- > right);} / / construct a tree $node5=new TreeNode (5); $node7=new TreeNode (7); $node9=new TreeNode (9); $node11=new TreeNode (11); $node6=new TreeNode (6); $node10=new TreeNode (10); $node8=new TreeNode (8); $node8- > left=$node6;$node8- > right=$node10;$node6- > left=$node5;$node6- > right=$node7;$node10- > left=$node9;$node10- > right=$node11;$tree=$node8;// Mirror this binary tree var_dump ($tree) Mirror ($tree); var_dump ($tree) Object (TreeNode) # 7 (3) {["val"] = > int (8) ["left"] = > object (TreeNode) # 5 (3) {["val"] = > int (6) ["left"] = > object (TreeNode) # 1 (3) {["val"] = > int (5) ["left"] = > NULL ["right"] = > NULL} ["right" "] = > object (TreeNode) # 2 (3) {[" val "] = > int (7) [" left "] = > NULL [" right "] = > NULL}} [" right "] = > object (TreeNode) # 6 (3) {[" val "] = > int (10) [" left "] = > object (TreeNode) # 3 (3) {[" val "] = > Int (9) ["left"] = > NULL ["right"] = > NULL} ["right"] = > object (TreeNode) # 4 (3) {["val"] = > int (11) ["left"] = > NULL ["right"] = > NULL} object (TreeNode) # 7 (3) {["val"] = > int (8) ["left"] = > object (TreeNode) # 6 (3) {["val"] = > int (10) ["left"] = > object (TreeNode) # 4 (3) {["val"] = > int (11) ["left"] = > NULL ["right"] = > NULL} ["right"] = > object (TreeNode) # 3 (3) {[val "] = > int (9) ["left"] = > NULL ["right"] = > NULL}} ["right"] = > object (TreeNode) # 5 (3) {["val"] = > int (6) [left "] = > object (TreeNode) # 2 (3) {[" val "] = > int (7) [" left "] = > NULL [" right " "] = > NULL} [" right "] = > object (TreeNode) # 1 (3) {[" val "] = > int (5) [" left "] = > NULL [" right "] = > NULL}} so far On the "PHP how to achieve mirror binary tree" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Mirror subtree learning more help practical next ideas articles methods theories knowledge articles websites materials follow questions easy to use recursion transformation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Docker Shulou Tech Info NVidia