PHP 输入一棵二叉树和一个数字n,要求找出路径和为n的所有路径

 1 <?php
 2     #输入一棵二叉树和一个数字n,要求找出路径和为n的所有路径
 3 
 4     class Node {
 5         public $data = null;
 6         public $parent = null;
 7         public $left = null;
 8         public $right = null;
 9     }
10 
11     #使用数组构造完全二叉树
12     function build_cbtree($a) {
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     #插入完全二叉树节点
26     function insert_node($root, $inode) {
27         #使用树的广度优先遍历顺序取出节点,直到找到第一个左右子节点没满的节点,将待插入节点插入节点左边或右边
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     #树的广度优先遍历 
53     function 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                 #继续递归
74                 #此处paths是传值调用,所以可以算出多条路径而互不影响
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                 #输出路径
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); #广度优先遍历
90     $b = array();
91     get_paths($root, $b, 25); #输出路径和为25的路径
92 ?>

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值