鱼C论坛

 找回密码
 立即注册
查看: 1094|回复: 1

[技术交流] C++刷剑指offer(面试题32 - I. 从上到下打印二叉树)【广度优先搜索】

[复制链接]
发表于 2020-4-2 23:59:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-4-6 13:04 编辑

题目描述:
  1. 从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

  2.  

  3. 例如:
  4. 给定二叉树: [3,9,20,null,null,15,7],

  5.     3
  6.    / \
  7.   9  20
  8.     /  \
  9.    15   7
  10. 返回:

  11. [3,9,20,15,7]
  12.  

  13. 提示:

  14. 节点总数 <= 1000

  15. 来源:力扣(LeetCode)
  16. 链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof
  17. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码

  1. #include<iostream>
  2. #include <malloc.h>
  3. #include <vector>
  4. #include <math.h>
  5. #include <queue>

  6. using namespace std;

  7. struct TreeNode{
  8.         int val;
  9.         TreeNode* left;
  10.         TreeNode* right;
  11.         TreeNode(int x): val(x), left(NULL), right(NULL){
  12.         }
  13. };

  14. TreeNode* CreateTree(vector<int> input){
  15.         TreeNode* res = (TreeNode*)malloc(sizeof(TreeNode)*input.size());
  16.         for(int i = 0; i < input.size(); i++){
  17.                 res[i].val = input[i];
  18.                 res[i].left = NULL;
  19.                 res[i].right = NULL;
  20.         }
  21.         for(int i= 0; i < input.size(); i++){
  22.                 if(2*i+1 < input.size()){
  23.                         res[i].left = &res[2*i+1];
  24.                 }
  25.                 if(2*i+2 < input.size()){
  26.                         res[i].right = &res[2*i+2];
  27.                 }
  28.                
  29.         }
  30.         return res;
  31.        
  32. }

  33. void middle(TreeNode* root, vector<vector<int> >& res, int left, int right, int depth){
  34.     if(root == NULL) return;
  35.     int insert = left + (right - left) / 2;
  36.     res[depth][insert] = root->val;
  37.            
  38.     middle(root->left, res, left, insert - 1, depth + 1);
  39.     middle(root->right, res, insert + 1, right, depth + 1);
  40.     }

  41. int treeDepth(TreeNode* root){
  42.     if(root == NULL || root -> val == 0) return 0;
  43.     return max(treeDepth(root->left) + 1, treeDepth(root->right) + 1);
  44. }
  45.       
  46. void printTree(TreeNode* root) {
  47.     int depth = treeDepth(root);
  48.     int width = pow(2, depth) - 1;
  49.     vector<vector<int> > res(depth, vector<int>(width, 0));
  50.     middle(root, res, 0, width - 1, 0);
  51.     for(int i = 0; i < res.size(); i++){
  52.                 for(int j = 0; j < res[i].size();j++){
  53.                         if(res[i][j] == 0){
  54.                                 cout  << " ";
  55.                         }
  56.                         else{
  57.                                 cout << res[i][j];
  58.                         }
  59.                        
  60.                 }
  61.                 cout << endl;
  62.         }
  63.         cout << "------------------" << endl;
  64.     }


  65. vector<int> solution(TreeNode* root) {
  66.         vector<int> res;
  67.         if(root == NULL) return res;
  68.         queue <TreeNode*> temp;
  69.         temp.push(root);
  70.         while(!temp.empty()){
  71.             auto node = temp.front();
  72.             temp.pop();
  73.             res.push_back(node -> val);
  74.             if(node -> left != NULL) temp.push(node -> left);
  75.             if(node -> right != NULL) temp.push(node -> right);
  76.         }
  77.         return res;
  78. }





  79. int main(void){
  80.         vector<int> input;
  81.         cout << "send numbers for the tree" << endl;
  82.         int number;
  83.         while(cin >> number){
  84.                 input.push_back(number);
  85.         }
  86.          
  87.         TreeNode* root = CreateTree(input);
  88.         printTree(root);
  89.         vector<int> res  = solution(root);
  90.         for(int i = 0; i < res.size(); i++){
  91.                 if(res[i] != 0){
  92.                         cout << res[i] << " ";
  93.                 }
  94.                
  95.         }
  96.        
  97.         return 0;
  98. }
复制代码



注意事项:
1.用到广度优先搜索。
2.广度优先搜索一般使用队列辅助求解。
3.参考链接:https://leetcode-cn.com/problems ... xia-da-yin-er-ch-4/

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-29 12:17:49 | 显示全部楼层
新增代码
  1. vector<int> solution(TreeNode* root) {
  2.         vector<int> res;
  3.         if(root == NULL) return res;
  4.         queue <TreeNode*> temp;
  5.         temp.push(root);
  6.         while(!temp.empty()){
  7.             auto node = temp.front();
  8.             temp.pop();
  9.             res.push_back(node -> val);
  10.             if(node -> left != NULL) temp.push(node -> left);
  11.             if(node -> right != NULL) temp.push(node -> right);
  12.         }
  13.         return res;
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 09:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表