@@ -48,58 +48,58 @@ func isSubtree(root *TreeNode, subRoot *TreeNode) bool {
4848 (This complexity is determined. because the maximun sizes of the queues (` q ` , ` q1 ` , ` q2 ` ), which doesn't exceed the sum of sizes of both trees.)
4949<!-- Add your space complexity here, e.g. $$O(n)$$ -->
5050# Code
51- ```
51+ ``` go
5252func isEqualTree (root *TreeNode , subRoot *TreeNode ) bool {
53- q1 := []*TreeNode{root}
54- q2 := []*TreeNode{subRoot}
53+ q1 := []*TreeNode{root}
54+ q2 := []*TreeNode{subRoot}
5555
56- for len(q1) != 0 {
57- f1 := q1[0]
58- f2 := q2[0]
56+ for len (q1) != 0 {
57+ f1 := q1[0 ]
58+ f2 := q2[0 ]
5959
60- q1 = q1[1:]
61- q2 = q2[1:]
60+ q1 = q1[1 :]
61+ q2 = q2[1 :]
6262
63- if (f1 == nil) && (f2 == nil) {
64- continue
65- }
66- if (f1 == nil) || (f2 == nil) || (f1.Val != f2.Val) {
67- return false
68- }
63+ if (f1 == nil ) && (f2 == nil ) {
64+ continue
65+ }
66+ if (f1 == nil ) || (f2 == nil ) || (f1.Val != f2.Val ) {
67+ return false
68+ }
6969
70- q1 = append(q1, f1.Left)
71- q1 = append(q1, f1.Right)
70+ q1 = append (q1, f1.Left )
71+ q1 = append (q1, f1.Right )
7272
73- q2 = append(q2, f2.Left)
74- q2 = append(q2, f2.Right)
75- }
76-
77- return true
73+ q2 = append (q2, f2.Left )
74+ q2 = append (q2, f2.Right )
75+ }
76+
77+ return true
7878}
7979
8080func isSubtree (root *TreeNode , subRoot *TreeNode ) bool {
81- if root == nil {
82- //assert subRoot != nil
83- return false
84- }
81+ if root == nil {
82+ // assert subRoot != nil
83+ return false
84+ }
8585
86- q := []*TreeNode{root}
86+ q := []*TreeNode{root}
8787
88- for len(q) != 0 {
89- node := q[0]
90- q = q[1:]
88+ for len (q) != 0 {
89+ node := q[0 ]
90+ q = q[1 :]
9191
92- if node == nil {
93- continue
94- }
95- if isEqualTree(node, subRoot) {
96- return true
97- }
92+ if node == nil {
93+ continue
94+ }
95+ if isEqualTree (node, subRoot) {
96+ return true
97+ }
9898
99- q = append(q, node.Left)
100- q = append(q, node.Right)
101- }
99+ q = append (q, node.Left )
100+ q = append (q, node.Right )
101+ }
102102
103- return false
103+ return false
104104}
105105```
0 commit comments