forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTree2List.java
More file actions
57 lines (48 loc) · 1.18 KB
/
Copy pathTree2List.java
File metadata and controls
57 lines (48 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package ByteDancePreparation;
import java.util.LinkedList;
/**
* @Author MaoTian
* @Classname Tree2List
* @Description 将二叉搜索树转换为双向链表,使用递归和非递归的方式
* @Date 下午9:09 2019/9/14
* @Version 1.0
* @Created by mao<[email protected]>
*/
class TreeNode{
int val;
TreeNode left;
TreeNode right;
}
public class Tree2List {
//递归
public TreeNode convert2(TreeNode root){
TreeNode head=null;
return head;
}
public void travel(TreeNode root){
}
//非递归
public TreeNode convert1(TreeNode root){
TreeNode head=null;
TreeNode pre=null;
LinkedList<TreeNode> tmp=new LinkedList<>();
while (root!=null || !tmp.isEmpty()){
//左边
while (root!=null){
tmp.push(root);
root=root.left;
}
TreeNode node=tmp.pop();
if(head==null){
head=node;
pre=node;
}else {
node.left=pre;
pre.right=node;
pre=node;
}
root=root.right;
}
return head;
}
}