File tree Expand file tree Collapse file tree 5 files changed +83
-7
lines changed Expand file tree Collapse file tree 5 files changed +83
-7
lines changed Original file line number Diff line number Diff line change 1
1
94. 二叉树的中序遍历
2
2
102. 二叉树的层序遍历
3
3
144. 二叉树的前序遍历
4
- 145. 二叉树的后序遍历
4
+ 145. 二叉树的后序遍历
5
+ 589. N叉树的前序遍历
Original file line number Diff line number Diff line change 22
22
* 递归思路:
23
23
* 1、定义数据结构:使用列表成员变量,存储每次递归操作存入的值
24
24
* 2、递归终止条件:节点为空时返回
25
- * 3、主要数据操作 :把节点的值存入列表
25
+ * 3、单层递归逻辑 :把节点的值存入列表
26
26
* 4、递归逻辑:
27
- * 左右节点需要与根节点做同样的事,就要用递归
27
+ * 左右节点需要与根节点做同样的事,就要调同样的方法,即递归
28
28
* 确定递归顺序/遍历顺序,左中右
29
29
* 每层不需要接收使用递归方法返回值,列表成员变量存储了结果
30
30
* */
Original file line number Diff line number Diff line change 22
22
* 递归思路:
23
23
* 1、定义数据结构:使用列表成员变量,存储每次递归操作存入的值
24
24
* 2、递归终止条件:节点为空时返回
25
- * 3、主要数据操作 :把节点的值存入列表
25
+ * 3、单层递归逻辑 :把节点的值存入列表
26
26
* 4、递归逻辑:
27
- * 左右节点需要与根节点做同样的事,就要用递归
27
+ * 左右节点需要与根节点做同样的事,就要调同样的方法,即递归
28
28
* 确定递归顺序/遍历顺序,中左右
29
29
* 每层不需要接收使用递归方法返回值,列表成员变量存储了结果
30
30
* */
Original file line number Diff line number Diff line change 22
22
* 递归思路:
23
23
* 1、定义数据结构:使用列表成员变量,存储每次递归操作存入的值
24
24
* 2、递归终止条件:节点为空时返回
25
- * 3、主要数据操作 :把节点的值存入列表
25
+ * 3、单层递归逻辑 :把节点的值存入列表
26
26
* 4、递归逻辑:
27
- * 左右节点需要与根节点做同样的事,就要用递归
27
+ * 左右节点需要与根节点做同样的事,就要调同样的方法,即递归
28
28
* 确定递归顺序/遍历顺序,左右中
29
29
* 每层不需要接收使用递归方法返回值,列表成员变量存储了结果
30
30
* */
Original file line number Diff line number Diff line change
1
+ // N叉树的前序遍历
2
+
3
+
4
+ /*
5
+ // Definition for a Node.
6
+ class Node {
7
+ public int val;
8
+ public List<Node> children;
9
+
10
+ public Node() {}
11
+
12
+ public Node(int _val) {
13
+ val = _val;
14
+ }
15
+
16
+ public Node(int _val, List<Node> _children) {
17
+ val = _val;
18
+ children = _children;
19
+ }
20
+ };
21
+ */
22
+
23
+
24
+ /*
25
+ * 递归思路:
26
+ * 1、定义数据结构:使用列表成员变量,存储每次递归操作存入的值
27
+ * 2、递归终止条件:节点为空时返回
28
+ * 3、单层递归逻辑:把节点的值存入列表
29
+ * 4、递归逻辑:
30
+ * 子节点需要与根节点做同样的事,就要调同样的方法,即递归
31
+ * 确定递归顺序/遍历顺序,中左右,子节点从左到右
32
+ * 每层不需要接收使用递归方法返回值,列表成员变量存储了结果
33
+ * */
34
+ class Solution {
35
+ public List <Integer > list = new ArrayList <>();
36
+ public List <Integer > preorder (Node root ) {
37
+ if (root == null ) {
38
+ return list ;
39
+ }
40
+ list .add (root .val );
41
+ for (Node node : root .children ) {
42
+ preorder (node );
43
+ }
44
+ return list ;
45
+ }
46
+ }
47
+
48
+
49
+ /*
50
+ * 迭代思路:
51
+ * 1、定义数据结构:列表存放节点值,栈按序存放节点
52
+ * 2、遍历条件、操作逻辑:
53
+ * 1)节点入栈顺序决定了节点的遍历顺序,栈的后进先出特点,想实现前序遍历,子节点要从右到左入栈,出栈从左到右操作节点,先存储后操作
54
+ * 2)根节点入栈初始化,轮询栈,弹出节点,节点值存入列表,子节点从右到左入栈
55
+ * */
56
+ class Solution {
57
+ public List <Integer > preorder (Node root ) {
58
+ List <Integer > list = new ArrayList <>();
59
+ if (root == null ) {
60
+ return list ;
61
+ }
62
+ Stack <Node > stack = new Stack <>();
63
+ stack .push (root );
64
+ while (!stack .isEmpty ()) {
65
+ root = stack .pop ();
66
+ list .add (root .val );
67
+ List <Node > children = root .children ;
68
+ Collections .reverse (children );
69
+ for (Node node : children ) {
70
+ stack .push (node );
71
+ }
72
+ }
73
+ return list ;
74
+ }
75
+ }
You can’t perform that action at this time.
0 commit comments