1+ from collections import defaultdict
2+ from typing import List , Optional
3+ from create import Solution , TreeNode
4+
5+ # Helper functions for testing
6+ def tree_to_list (root : Optional [TreeNode ]) -> List [Optional [int ]]:
7+ """Convert tree to level-order list representation (BFS)"""
8+ if not root :
9+ return []
10+
11+ result = []
12+ queue = [root ]
13+
14+ while queue :
15+ node = queue .pop (0 )
16+ if node :
17+ result .append (node .val )
18+ queue .append (node .left )
19+ queue .append (node .right )
20+ else :
21+ result .append (None )
22+
23+ # Remove trailing None values
24+ while result and result [- 1 ] is None :
25+ result .pop ()
26+
27+ return result
28+
29+
30+ def print_tree (root : Optional [TreeNode ], level = 0 , prefix = "Root: " ):
31+ """Print tree structure in a readable format"""
32+ if root is not None :
33+ print (" " * (level * 4 ) + prefix + str (root .val ))
34+ if root .left or root .right :
35+ if root .left :
36+ print_tree (root .left , level + 1 , "L--- " )
37+ else :
38+ print (" " * ((level + 1 ) * 4 ) + "L--- None" )
39+ if root .right :
40+ print_tree (root .right , level + 1 , "R--- " )
41+ else :
42+ print (" " * ((level + 1 ) * 4 ) + "R--- None" )
43+
44+
45+ def run_tests ():
46+ solution = Solution ()
47+
48+ # Test 1: Example 1 from problem
49+ print ("Test 1: Multiple levels with full tree" )
50+ descriptions1 = [[20 ,15 ,1 ],[20 ,17 ,0 ],[50 ,20 ,1 ],[50 ,80 ,0 ],[80 ,19 ,1 ]]
51+ root1 = solution .createBinaryTree (descriptions1 )
52+ result1 = tree_to_list (root1 )
53+ print (f"Input: { descriptions1 } " )
54+ print (f"Output: { result1 } " )
55+ print (f"Expected: [50, 20, 80, 15, 17, 19]" )
56+ print_tree (root1 )
57+ print ()
58+
59+ # Test 2: Example 2 from problem
60+ print ("Test 2: Linear tree with sparse nodes" )
61+ descriptions2 = [[1 ,2 ,1 ],[2 ,3 ,0 ],[3 ,4 ,1 ]]
62+ root2 = solution .createBinaryTree (descriptions2 )
63+ result2 = tree_to_list (root2 )
64+ print (f"Input: { descriptions2 } " )
65+ print (f"Output: { result2 } " )
66+ print (f"Expected: [1, 2, None, None, 3, 4]" )
67+ print_tree (root2 )
68+ print ()
69+
70+ # Test 3: Single parent with two children
71+ print ("Test 3: Simple tree - one parent, two children" )
72+ descriptions3 = [[1 ,2 ,1 ],[1 ,3 ,0 ]]
73+ root3 = solution .createBinaryTree (descriptions3 )
74+ result3 = tree_to_list (root3 )
75+ print (f"Input: { descriptions3 } " )
76+ print (f"Output: { result3 } " )
77+ print (f"Expected: [1, 2, 3]" )
78+ print_tree (root3 )
79+ print ()
80+
81+ # Test 4: Only left children
82+ print ("Test 4: Only left children chain" )
83+ descriptions4 = [[1 ,2 ,1 ],[2 ,3 ,1 ],[3 ,4 ,1 ]]
84+ root4 = solution .createBinaryTree (descriptions4 )
85+ result4 = tree_to_list (root4 )
86+ print (f"Input: { descriptions4 } " )
87+ print (f"Output: { result4 } " )
88+ print (f"Expected: [1, 2, None, 3, None, 4]" )
89+ print_tree (root4 )
90+ print ()
91+
92+ # Test 5: Only right children
93+ print ("Test 5: Only right children chain" )
94+ descriptions5 = [[1 ,2 ,0 ],[2 ,3 ,0 ],[3 ,4 ,0 ]]
95+ root5 = solution .createBinaryTree (descriptions5 )
96+ result5 = tree_to_list (root5 )
97+ print (f"Input: { descriptions5 } " )
98+ print (f"Output: { result5 } " )
99+ print (f"Expected: [1, None, 2, None, 3, None, 4]" )
100+ print_tree (root5 )
101+ print ()
102+
103+ # Test 6: Single node
104+ print ("Test 6: Single parent-child relationship" )
105+ descriptions6 = [[1 ,2 ,1 ]]
106+ root6 = solution .createBinaryTree (descriptions6 )
107+ result6 = tree_to_list (root6 )
108+ print (f"Input: { descriptions6 } " )
109+ print (f"Output: { result6 } " )
110+ print (f"Expected: [1, 2]" )
111+ print_tree (root6 )
112+ print ()
113+
114+ # Test 7: Larger values
115+ print ("Test 7: Large node values" )
116+ descriptions7 = [[100000 ,50000 ,1 ],[100000 ,75000 ,0 ],[50000 ,25000 ,1 ]]
117+ root7 = solution .createBinaryTree (descriptions7 )
118+ result7 = tree_to_list (root7 )
119+ print (f"Input: { descriptions7 } " )
120+ print (f"Output: { result7 } " )
121+ print (f"Expected: [100000, 50000, 75000, 25000]" )
122+ print_tree (root7 )
123+ print ()
124+
125+ # Test 8: Unordered descriptions
126+ print ("Test 8: Descriptions in random order" )
127+ descriptions8 = [[3 ,4 ,0 ],[1 ,2 ,1 ],[2 ,3 ,0 ]]
128+ root8 = solution .createBinaryTree (descriptions8 )
129+ result8 = tree_to_list (root8 )
130+ print (f"Input: { descriptions8 } " )
131+ print (f"Output: { result8 } " )
132+ print (f"Expected: [1, 2, None, None, 3, None, 4]" )
133+ print_tree (root8 )
134+ print ()
135+
136+
137+ if __name__ == "__main__" :
138+ run_tests ()
0 commit comments