File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .algorithm .ok .week_2 ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .Collections ;
7+ import java .util .LinkedList ;
8+ import java .util .List ;
9+
10+ /***
11+ * 429. N叉树的层序遍历
12+ */
13+ public class N_tree_sequence {
14+ @ Test
15+ public void main () {
16+
17+ }
18+
19+ public List <List <Integer >> levelOrder (Node root ) {
20+ List <Node > children = new ArrayList <>();
21+ List <List <Integer >> li = new ArrayList <>();
22+ if (root == null ) {
23+ return li ;
24+ }
25+ children .add (root );
26+ test (children ,li );
27+ return li ;
28+ }
29+
30+ private void test (List <Node > children ,List <List <Integer >> li ) {
31+ List <Integer > list = new ArrayList <>();
32+ List <Node > childrenNode = new ArrayList <>();
33+ if (children != null && children .size () > 0 ) {
34+ for (int i = 0 ;i < children .size ();i ++){
35+ list .add (children .get (i ).val );
36+ childrenNode .addAll (children .get (i ).children );
37+ }
38+ li .add (list );
39+ test (childrenNode ,li );
40+ }
41+
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ package com .algorithm .ok .week_2 ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .Collections ;
7+ import java .util .LinkedList ;
8+ import java .util .List ;
9+
10+ /***
11+ * 144. 二叉树的前序遍历
12+ */
13+ public class Two_tree_front {
14+ @ Test
15+ public void main () {
16+
17+ }
18+
19+ //递归遍历
20+ public List <Integer > preorderTraversal (TreeNode root ) {
21+ List <Integer > li = new ArrayList <>();
22+ if (root == null ) {
23+ return li ;
24+ }
25+ test (li ,root );
26+ return li ;
27+ }
28+
29+ private void test (List <Integer > li ,TreeNode root ) {
30+ li .add (root .val );
31+ if (root .left != null ){
32+ test (li ,root .left );
33+ }
34+ if (root .right != null ){
35+ test (li ,root .right );
36+ }
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments