微信扫一扫打赏支持

php实现把二叉树打印成多行(谋而后动,写好算法思路,不然浪费超多时间而且还是错误代码,而且精力消耗会导致代码正确率下降以及低级错误)

php实现把二叉树打印成多行(谋而后动,写好算法思路,不然浪费超多时间而且还是错误代码,而且精力消耗会导致代码正确率下降以及低级错误

一、总结

要点:a、层次遍历(队列)  b、层次遍历中的层次(孩子在父亲的层次上面加1)

另外一种:

1、求每一层的时候:用的是计算开始时当前队列中元素的个数,用两层while

谋而后动,写好算法思路,不然浪费超多时间而且还是错误代码,而且精力消耗会导致代码正确率下降以及低级错误

 

二、php实现把二叉树打印成多行

题目描述:

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。

 

三、代码

代码一:

 1 链接:https://www.nowcoder.com/questionTerminal/445c44d982d04483b04a54f298796288
 2 来源:牛客网
 3 
 4 层次遍历
 5  
 6 class Solution {
 7 public:
 8         vector<vector<int> > Print(TreeNode* pRoot) {
 9             vector<vector<int> > vec;
10             if(pRoot == NULL) return vec;
11  
12             queue<TreeNode*> q;
13             q.push(pRoot);
14  
15             while(!q.empty())
16             {
17                 int lo = 0, hi = q.size();
18                 vector<int> c;
19                 while(lo++ < hi)
20                 {
21                     TreeNode *t = q.front();
22                     q.pop();
23                     c.push_back(t->val);
24                     if(t->left) q.push(t->left);
25                     if(t->right) q.push(t->right);
26                 }
27                 vec.push_back(c);
28             }
29             return vec;
30         }
31 };
 1 <?php
 2  
 3 /*class TreeNode{
 4     var $val;
 5     var $left = NULL;
 6     var $right = NULL;
 7     function __construct($val){
 8         $this->val = $val;
 9     }
10 }*/
11 function MyPrint($pRoot)
12 {
13     // write code here
14     $q = new SplQueue();
15     if(!$pRoot){
16         return [];
17     }
18     $result = []; //1、$result[$i][]
19     $i=0;
20     $q->push($pRoot);
21     while(!$q->isEmpty()){
22         $count = $q->count(); //1、求每一层的时候:用的是计算开始时当前队列中元素的个数,用两层while
23         while($count--){
24             $t = $q->shift();
25             if($t){
26                 $result[$i][] = $t->val;
27                 $q->push($t->left);
28                 $q->push($t->right);
29             }
30         }
31         $i++;
32     }
33     return $result;
34 }

 

 

代码二:谋而后动,写好算法思路,不然浪费超多时间而且还是错误代码,而且精力消耗会导致代码正确率下降以及低级错误

 1 <?php
 2 
 3 /*class TreeNode{
 4     var $val;
 5     var $left = NULL;
 6     var $right = NULL;
 7     function __construct($val){
 8         $this->val = $val;
 9     }
10 }*/
11 //算法:树的层次遍历(队列
12 //这里每层一行输出,所以需要判断层次,所以每个节点加一个遍历判断层次即可
13 function MyPrint($pRoot){
14     if(!$pRoot) return [];
15     $preCenci=1;
16     $ansLine=array();
17     $ans=array();
18     $queue=array();//队列
19     $queCenci=array();//队列层次
20     array_push($queCenci,1);
21     array_push($queue,$pRoot);
22     while(!empty($queue)){
23         $node=array_shift($queue);
24         $cenci=array_shift($queCenci);
25         if($cenci!=$preCenci){
26             $ans[]=$ansLine;
27             $ansLine=array();
28             $ansLine[]=$node->val;
29             $preCenci=$cenci;
30         }else{
31             $ansLine[]=$node->val;
32             $preCenci=$cenci;
33         }
34         if($node->left) { array_push($queue,$node->left); array_push($queCenci,$cenci+1);}
35         if($node->right) { array_push($queue,$node->right); array_push($queCenci,$cenci+1);} //1、这里写成了$queue
36     }
37     return $ans; //2、函数里面就是return,而不是echo
38 }

 

posted @ 2018-05-21 17:59  范仁义  阅读(422)  评论(0编辑  收藏  举报