Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 77b4fdc

Browse files
Issue #194--Added : 102. Binary Tree Level Order Traversal (#198)
* Added : 102. Binary Tree Level Order Traversal * Update README.md
1 parent 76df15b commit 77b4fdc

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Problem:
3+
Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
4+
5+
Example 1:
6+
Input: root = [3,9,20,null,null,15,7]
7+
Output: [[3],[9,20],[15,7]]
8+
9+
Example 2:
10+
Input: root = [1]
11+
Output: [[1]]
12+
13+
Example 3:
14+
Input: root = []
15+
Output: []
16+
17+
Constraints:
18+
The number of nodes in the tree is in the range [0, 2000].
19+
-1000 <= Node.val <= 1000
20+
*/
21+
22+
23+
/*
24+
Space Complexity : O(N)
25+
Time Complexity : O(N)
26+
Difficulty level : Medium
27+
*/
28+
29+
class Solution {
30+
public:
31+
void fun( map<int,vector<int>>&mp, TreeNode* root, int level)
32+
{
33+
if(!root)
34+
return;
35+
36+
mp[level].push_back(root->val);
37+
38+
fun(mp,root->left,level+1);
39+
fun(mp,root->right,level+1);
40+
41+
}
42+
43+
vector<vector<int>> levelOrder(TreeNode* root) {
44+
vector<vector<int>>v;
45+
if(!root)
46+
return v;
47+
48+
map<int,vector<int>>mp;
49+
int level=0;
50+
fun(mp,root,level);
51+
auto it=mp.begin();
52+
while(it!=mp.end())
53+
{
54+
v.push_back(it->second);
55+
it++;
56+
}
57+
58+
return v;
59+
60+
}
61+
};

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
254254
| 968 | [Binary Tree Cameras](https://leetcode.com/problems/binary-tree-cameras/) | [C++](./C++/Binary-Tree-Cameras.cpp) | _O(n)_ | _O(logn)_ | Hard | Binary Tree, Dynamic Programming |
255255
| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [Javascript](./JavaScript/98.Validate-Binary-Search-Tree.js) | _O(log(n))_ | _O(log(n))_ | Medium | Binary Tree |
256256
| 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Java](./Java/Redundant-Connection/redundant-connection.java) | _O(N)_ | _O(N)_ | Medium | Tree, Union Find |
257+
| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) |[C++](./C++/Binary-Tree-Level-Order-Traversal.cpp)| _O(n)_ | _O(n)_ | Medium | Binary Tree, map | |
257258

258259
<br/>
259260
<div align="right">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /