二叉树的层次遍历

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<vector<int>> levelOrder(TreeNode* root) {
  13. vector<vector<int>> v;
  14. if(!root) return v;
  15. vector<int> t;
  16.  
  17. t.push_back(root->val);
  18. v.push_back(t);
  19. root->val = ;
  20.  
  21. queue<TreeNode*> q;
  22. q.push(root);
  23.  
  24. while(!q.empty()){
  25. TreeNode* now = q.front();
  26. q.pop();
  27. if(!now) continue;
  28.  
  29. q.push(now->left);
  30. q.push(now->right);
  31. if(now->val < v.size()){
  32. if(now->left) v[now->val].push_back(now->left->val);
  33. if(now->right) v[now->val].push_back(now->right->val);
  34. }
  35. else{
  36. vector<int> t;
  37. if(now->left) t.push_back(now->left->val);
  38. if(now->right) t.push_back(now->right->val);
  39. if(t.size() != )v.push_back(t);
  40. }
  41. if(now->left) now->left->val = now->val + ;
  42. if(now->right)now->right->val = now->val + ;
  43. }
  44. //reverse(v.begin(),v.end());
  45. return v;
  46. }
  47. };

Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS的更多相关文章

  1. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历

    基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...

  5. 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  6. Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. leetcode 102. Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  9. LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

随机推荐

  1. SecureCRT自动备份脚本-华为

    # $language = "VBScript" # $interface = "1.0" Sub Main Const ForReading = 1, For ...

  2. Ubuntu 14.04中gedit打开文件出现中文乱码问题

    http://blog.csdn.net/cywosp/article/details/32325449/ 在中文支持配置还不完整的Ubuntu 14.04中,使用gedit打开带有中文字符的文件有时 ...

  3. 传值 属性 block 单例 协议

    界面传值 四种传值的方式 1.属性传值(从前往后) 步骤: 1.属性传值用于第一个界面向第二个界面传值 2.明确二者联系的桥梁,也就是触发跳转的地方 3.明确传输的值,类型是什么 4.在第二个视图控制 ...

  4. [转] 基于ArcGISServer实现活动地图标注

    ——王嘉彬(Esri中国上海分公司) 1.背景 1.1.主流互联网地图应用的现状 在目前主流的互联网地图应用中,如 Google Map(图 1).搜狗地图(图2),POI 兴趣点的文字标注越来越多的 ...

  5. Python 迭代dict 效率

    迭代dict也要讲求效率,不然就要走进性能陷阱 以下三种迭代方式:keys,iterkeys, hashkey import timeit DICT_SIZE = 100 * 100000 testD ...

  6. berkeley db replica机制 - 主从同步

    repmgr/repmgr_net.c, __repmgr_send(): 做send_broadcast, 然后根据policy 对DB_REP_PERMANENT的处理 __repmgr_send ...

  7. 【08_238】Product of Array Except Self

    Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given ...

  8. 如何优化cocos2d程序的内存使用和程序大小:第一部分

    译者: 在我完成第一个游戏项目的时候,我深切地意识到“使用cocos2d来制作游戏的开发者们,他们大多会被cocos2d的内存问题所困扰”.而我刚开始接触cocos2d的时候,社区里面的人们讨论了一个 ...

  9. Adaboost算法初识

    1.算法思想很简单: AdaBoost 是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器,即弱分类器,然后把这些弱分类器集合起来,构造一个更强的最终分类器.(三个臭皮匠,顶个诸葛亮) 它的 ...

  10. 分分钟用上C#中的委托和事件之窗体篇

    上次以鸿门宴的例子写了一篇名为<分分钟用上C#中的委托和事件>的博文,旨在帮助C#初学者迈过委托和事件这道坎,能够用最快的速度掌握如何使用它们.如果觉得意犹未尽,或者仍然不知如何在实际应用 ...