leetcode || 102、Binary Tree Level Order Traversal

problem:

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},

    3
   /   9  20
    /     15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

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

Hide Tags

Tree Breadth-first
Search

题意:层序遍历二叉树,每层要分开

thinking:

(1)普通的二叉树层序遍历算法每层是没有分开的,这道题要求分开。

(2)采用广度优先搜索(BFS)的思想

(3)采用两个queue,递归实现每层分开的层序遍历,很经典

code:

class Solution {
  private:
      vector<vector<int> > ret;
  public:
      vector<vector<int> > levelOrder(TreeNode *root) {
          ret.clear();
          if(root==NULL)
              return ret;
          queue<TreeNode *> tmp_queue;
          tmp_queue.push(root);
          level_order(tmp_queue);
          return ret;
      }
      void level_order(queue<TreeNode *> queue1)
      {
          if(queue1.empty())
              return;
          vector<int> array;
          queue<TreeNode *> queue2;
          while(!queue1.empty())
          {
              TreeNode *tmp=queue1.front();
              array.push_back(tmp->val);
              queue1.pop();
              if(tmp->left!=NULL)
                  queue2.push(tmp->left);
              if(tmp->right!=NULL)
                  queue2.push(tmp->right);
          }
          ret.push_back(array);
          level_order(queue2);
      }
  };
时间: 2024-11-06 11:14:33

leetcode || 102、Binary Tree Level Order Traversal的相关文章

leetcode || 107、Binary Tree Level Order Traversal II

problem: 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}, 3 / 9 20 / 15 7 return its bottom-up level or

8.2 leetcode 102,107. binary tree level order traversal I &amp; II:(up-down &amp; bottom-up)

1 public class Solution { 2 public List<List<Integer>> levelOrder(TreeNode root) { 3 List<List<Integer>> res = new ArrayList<List<Integer>>(); 4 if(root == null) return res; 5 Queue<TreeNode> q = new LinkedList<

【LeetCode】107. Binary Tree Level Order Traversal II 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51508308 Subject 出处:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to rig

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 of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9  20 /  \ 15   7 return its level o

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 to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 二. 题目分析 由于使用了vector,这一题只需Binar

LeetCode OJ:Binary Tree Level Order Traversal(二叉树的层序遍历)

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}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 层序遍历,既

leetcode笔记:Binary Tree Level Order Traversal

一. 题目描述 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}, 3 / 9 20 / 15 7 二. 题目分析 无. 三. 示例代码 参考了Discussion中stellari的做法,递归进行层次遍历,并将每个

LeetCode OJ 107. Binary Tree Level Order Traversal 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}, 3 / 9 20 / 15 7 return its bottom-up level order traver

【LeetCode】107 - Binary Tree Level Order Traversal 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}, 3 / 9 20 / 15 7 return its bottom-up level order traver