Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a02a15d

Browse files
committed
Update readme.md
1 parent 58ee41f commit a02a15d

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
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)
3+
4+
Example:
5+
Input: root = [3,9,20,null,null,15,7]
6+
Output: [[3],[9,20],[15,7]]
7+
'''
8+
9+
10+
# Definition for a binary tree node.
11+
# class TreeNode:
12+
# def __init__(self, val=0, left=None, right=None):
13+
# self.val = val
14+
# self.left = left
15+
# self.right = right
16+
17+
18+
class Solution:
19+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
20+
# Recursive Solution
21+
# Time: O(n); Space: O(n)
22+
result = []
23+
if root is None:
24+
return result
25+
26+
def traverse(node, level):
27+
if node is None:
28+
return
29+
if len(result) == level:
30+
result.append([])
31+
32+
result[level].append(node.val)
33+
34+
traverse(node.left, level+1)
35+
traverse(node.right, level+1)
36+
37+
traverse(root, 0)
38+
return result
39+
40+
41+
class Solution:
42+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
43+
# Iterative Solution
44+
# Time: O(n); Space: O(n)
45+
result = [] # final result
46+
if root is None: return result
47+
queue = [] # for level order traversal
48+
queue.append(root)
49+
while(len(queue) > 0):
50+
size = len(queue)
51+
currLevel = []
52+
while(size > 0):
53+
size -= 1
54+
curr = queue.pop(0)
55+
currLevel.append(curr.val)
56+
if curr.left is not None:
57+
queue.append(curr.left)
58+
if curr.right is not None:
59+
queue.append(curr.right)
60+
result.append(currLevel)
61+
return result
62+

coding_solutions/DataStructure_related/Tree/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
| 1 | [Maximum Depth of Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/DataStructure_related/Tree/MaxDepthTree.py) |
66
| 2 | [Check Validity of BST](https://leetcode.com/problems/validate-binary-search-tree/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/DataStructure_related/Tree/CheckValidityBST.py) |
77
| 2 | [Check Symmetricity of Binary Tree](https://leetcode.com/problems/symmetric-tree/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/DataStructure_related/Tree/CheckSymmetryTree.py) |
8+
| 2 | [Check Symmetricity of Binary Tree](https://leetcode.com/problems/symmetric-tree/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/DataStructure_related/Tree/CheckSymmetryTree.py) |
89

910

0 commit comments

Comments
 (0)