File tree 2 files changed +46
-0
lines changed
0102.Binary Tree Level Order Traversal
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode:
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.left = None
6
+ # self.right = None
7
+
8
+ class Solution :
9
+ def level (self , nodes ):
10
+ """
11
+ :type nodes: List[TreeNode]
12
+ :rtype: List[List[int]]
13
+ """
14
+ Ret = [[]]
15
+ Next_Level = []
16
+
17
+ for each in nodes :
18
+ Ret [0 ].append (each .val )
19
+
20
+ if (each .left != None ):
21
+ Next_Level .append (each .left )
22
+ if (each .right != None ):
23
+ Next_Level .append (each .right )
24
+
25
+ if Next_Level :
26
+ Ret .extend (self .level (Next_Level ))
27
+ return Ret
28
+
29
+ def levelOrder (self , root ):
30
+ """
31
+ :type root: TreeNode
32
+ :rtype: List[List[int]]
33
+ """
34
+
35
+ if (root != None ):
36
+ return self .level ([root ])
37
+ else :
38
+ return []
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def hammingWeight (self , n ):
3
+ """
4
+ :type n: int
5
+ :rtype: int
6
+ """
7
+
8
+ return bin (n ).count ("1" )
You can’t perform that action at this time.
0 commit comments