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]
]

层序遍历,既可以用dfs实现也可以用bfs实现,用bfs实现的时候应该借助队列,代码如下:

dfs:

 1 class Solution {
 2 public:
 3     vector<vector<int>> levelOrder(TreeNode* root) {
 4         dfs(root, 0);
 5         return ret;
 6     }
 7     void dfs(TreeNode * root, int dep)
 8     {
 9         if(!root) return;
10         if(dep < ret.size()){
11             ret[dep].push_back(root->val);
12         }else{
13             vector<int> tmp;
14             tmp.push_back(root->val);
15             ret.push_back(tmp);
16         }
17         dfs(root->left, dep+1);
18         dfs(root->right, dep+1);
19     }
20 private:
21     vector<vector<int>> ret;
22 };

bfs:

 1 class Solution {
 2 private:
 3     struct Node{
 4         TreeNode * treeNode;
 5         int level;
 6         Node(){}
 7         Node(TreeNode * node, int lv)
 8         :treeNode(node), level(lv){}
 9     };
10 public:
11     vector<vector<int>> levelOrder(TreeNode* root) {
12         queue<Node> nodeQueue;
13         vector<vector<int>> ret;
14         if(root == NULL)
15             return ret;
16         nodeQueue.push(Node(root, 0));
17         int dep = -1;
18         while(!nodeQueue.empty()){
19             Node node = nodeQueue.front();
20             if(node.treeNode->left)
21                 nodeQueue.push(Node(node.treeNode->left, node.level + 1));
22             if(node.treeNode->right)
23                 nodeQueue.push(Node(node.treeNode->right, node.level + 1));
24             if(dep == node.level)
25                 ret[dep].push_back(node.treeNode->val);
26             else{
27                 vector<int> tmp;
28                 dep++;
29                 ret.push_back(tmp);
30                 ret[dep].push_back(node.treeNode->val);
31             }
32             nodeQueue.pop();
33         }
34         return ret;
35     }
36 };
时间: 2024-11-08 19:17:04

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

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 return its level order traversal as: [ [3], [9,20], [15,7] ] co

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

二叉树的层次遍历 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<in

lintcode 中等题:binary tree level order traversal 二叉树的层数遍历

题目 二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / 9 20 / 15 7 返回他的分层遍历结果: [ [3], [9,20], [15,7] ] 挑战 挑战1:只使用一个队列去实现它 挑战2:用DFS算法来做 解题  队列很容易,先加入,然后取出来的同时加入左右孩子节点 在剑指offer中有个题目和这个很类似,其只是层次遍历二叉树,没有要求把每层的节点单独放在一起的. 上面说的规律:每一次打印一

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

给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回其层次遍历结果为:[  [3],  [9,20],  [15,7]]详见:https://leetcode.com/problems/binary-tree-level-order-traversal/description/ /** * Definition for

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

翻译 给定一个二叉树,返回按层级顺序遍历的每个节点的值. 从左到右,逐层遍历. 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回它的层级顺序遍历结果为: [ [3], [9,20], [15,7] ] 翻译 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For

Leetcode 树 Binary Tree Level Order Traversal II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Level Order Traversal II Total Accepted: 10080 Total Submissions: 32610 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, l

Leetcode 树 Binary Tree Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Level Order Traversal Total Accepted: 12292 Total Submissions: 40417 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level

Java for 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 trave

【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 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用