PHP inputs a binary tree and a number N, which requires finding the path and all the paths for N.

Source: Internet
Author: User
1 <? PHP 2 # input a binary tree and a number n. It is required to find the path and all the paths for n. 3 4 class node {5 Public $ DATA = NULL; 6 Public $ parent = NULL; 7 public $ left = NULL; 8 Public $ right = NULL; 9} 10 11 # use an array to construct a Complete Binary Tree 12 function build_cbtree ($) {13 $ root = new node (); 14 $ root-> DATA = $ A [0]; 15 16 for ($ I = 1; $ I <count ($ A); $ I ++) {17 $ node = new node (); 18 $ node-> DATA = $ A [$ I]; 19 insert_node ($ root, $ node); 20} 21 22 return $ root; 23} 24 25 # insert completely two Function insert_node ($ root, $ inode) {27 # retrieve the node in the ascending order of the tree until the first left and right subnodes are found, insert 28 $ queue = array (); 29 array_unshift ($ queue, $ root); 30 31 While (! Empty ($ Queue) {32 $ cNode = array_pop ($ Queue); 33 if ($ cNode-> left = NULL) {34 $ cNode-> left = $ inode; 35 $ inode-> parent = $ cNode; 36 return $ root; 37} else {38 array_unshift ($ queue, $ cNode-> left ); 39} 40 if ($ cNode-> right = NULL) {41 $ cNode-> right = $ inode; 42 $ inode-> parent = $ cNode; 43 return $ root; 44} else {45 array_unshift ($ queue, $ cNode-> right); 46} 47} 48 49 return $ root; 50} 51 52 # tree breadth first traversal 53 F Unction bf_traverse ($ root) {54 $ queue = array (); 55 array_unshift ($ queue, $ root); 56 57 while (! Empty ($ Queue) {58 $ cNode = array_pop ($ Queue); 59 echo $ cNode-> data. ""; 60 if ($ cNode-> left! = NULL) array_unshift ($ queue, $ cNode-> left); 61 if ($ cNode-> right! = NULL) array_unshift ($ queue, $ cNode-> right); 62} 63 64 echo "<br>"; 65} 66 67 function get_paths ($ root, $ paths, $ sum) {68 if ($ root! = NULL) {69 $ sum-= $ root-> data; 70 $ paths [] = $ root-> data; 71 72 if ($ sum> 0) {73 # continue recursion 74 # Here paths is a value transfer call, so you can calculate multiple paths without affecting 75 if ($ root-> left! = NULL) get_paths ($ root-> left, $ paths, $ sum); 76 if ($ root-> right! = NULL) get_paths ($ root-> right, $ paths, $ sum); 77} else if ($ sum = 0) {78 # output path 79 foreach ($ paths as $ Val) {80 echo $ Val. ""; 81} 82 echo "<br>"; 83} 84} 85} 86 87 $ A = array (9, 8, 7, 6, 8, 4, 3, 2, 1); 88 $ root = build_cbtree ($ A); 89 bf_traverse ($ root); # breadth first traversal 90 $ B = array (); 91 get_paths ($ root, $ B, 25); # The output path is 92 for 25?>

9 8 7 6 8 4 3 2 1
9 8 6 2
9 8 8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.