I

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

For example:
Given binary tree{3,9,20,#,#,15,7},

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its level order traversal as:

  1. [
  2. [3],
  3. [9,20],
  4. [15,7]
  5. ]

confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

  1. 1
  2. / \
  3. 2 3
  4. /
  5. 4
  6. \
  7. 5

The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".

  1. /**
  2. * Definition for binary tree
  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>> res;
  14. if(root==NULL) return res;
  15. queue<TreeNode*> q;
  16. q.push(root);
  17. while(!q.empty()){
  18. int n=q.size();
  19. vector<int> v;
  20. for(int i=;i<n;i++){
  21. TreeNode *cur=q.front();
  22. q.pop();
  23. v.push_back(cur->val);
  24. if(cur->left!=NULL)
  25. q.push(cur->left);
  26. if(cur->right!=NULL)
  27. q.push(cur->right);
  28. }
  29. res.push_back(v);
  30. }
  31. return res;
  32. }
  33. };

II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree{3,9,20,#,#,15,7},

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its bottom-up level order traversal as:

  1. [
  2. [15,7]
  3. [9,20],
  4. [3],
  5. ]

confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.

先将结果v存入stack中,最后在从stack倒入res形成倒序,未找到其他好的方法

  1. /**
  2. * Definition for binary tree
  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> > levelOrderBottom(TreeNode *root) {
  13. vector<vector<int>> res;
  14. if(root==NULL) return res;
  15. stack<vector<int>> s;
  16. queue<TreeNode*> q;
  17. q.push(root);
  18. while(!q.empty()){
  19. int n=q.size();
  20. vector<int> v;
  21. for(int i=;i<n;i++){
  22. TreeNode *cur=q.front();
  23. q.pop();
  24. v.push_back(cur->val);
  25. if(cur->left!=NULL)
  26. q.push(cur->left);
  27. if(cur->right!=NULL)
  28. q.push(cur->right);
  29.  
  30. }
  31. s.push(v);
  32. }
  33. while(!s.empty()){
  34. res.push_back(s.top());
  35. s.pop();
  36. }
  37. return res;
  38. }
  39. };

binary-tree-level-order-traversal I、II——输出二叉树的数字序列的更多相关文章

  1. 【leetcode】Binary Tree Level Order Traversal I & II

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

  2. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  3. Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>

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

  4. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  5. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  6. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  7. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

  9. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  10. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

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

随机推荐

  1. Leetcode 482.密钥格式化

    密钥格式化 给定一个密钥字符串S,只包含字母,数字以及 '-'(破折号).N 个 '-' 将字符串分成了 N+1 组.给定一个数字 K,重新格式化字符串,除了第一个分组以外,每个分组要包含 K 个字符 ...

  2. AtCoder Grand Contest 022

    A - Diverse Word Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Gotou ...

  3. equal(),hashcode(),toString()方法的作用

    equal(),hashcode(),toString()方法的作用 这三个方法都是java.lang.Object的方法. equal();判断两对象是否相等hashcode();为对象在容器中添加 ...

  4. hdu5441

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  5. 一个关于python装饰器参数的问题

    看到廖雪峰python教程上,python装饰器一章 https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3 ...

  6. iOS学习笔记40-日志重定向

    一.日志重定向 我们在iOS开发过程中,我们时常会使用NSLog打印到控制台的日志信息进行代码调试,但这样调试的前提是连接上Xcode.如果进行真机调试但同时又不能连接Xcode的时候,就不能直接在x ...

  7. 【bzoj4568】[Scoi2016]幸运数字 树上倍增+高斯消元动态维护线性基

    题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征.一些旅行者希望游 ...

  8. 本博客由CSDN迁移而来,以前的博文可能显示不正常

    如题,原博客地址 http://blog.csdn.net/vicjiao 或点击右侧友链

  9. 学习 JSP:第三步 JSP基础(未完)

    因为之前学过也用过JSP,这里只列出笔记,初学者请移步其他教程. JSP隐含对象 JSP支持九个自动定义的变量,江湖人称隐含对象.这九个隐含对象的简介见下表: 对象 描述 request HttpSe ...

  10. 如何从sql server导出到csv文件

    如何从sql server导出到csv文件,具体代码如下: private static void WriteHeader(SqlDataReader reader, TextWriter outpu ...