Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Mirror binary Tree with PHP

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report