Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
100 lines (89 loc) · 2.26 KB

File metadata and controls

100 lines (89 loc) · 2.26 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package leetcode.common;
import java.util.Stack;
/**
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class TreeUtils {
public static int k=0;
public static int k2=0;
/**
* 根据string创建树:节点的值小于等于9
*
*
* @param s 先序的序列,空使用#表示
* @return
*/
public static TreeNode stringToTree(String s){
k=0;
TreeNode root=null;
root=createTree(root,s);
return root;
}
private static TreeNode createTree(TreeNode node,String s){
char ch=s.charAt(k++);
if(ch=='#') return null;
else{
node=new TreeNode(ch-'0');
node.left=createTree(node.left,s);
node.right=createTree(node.right,s);
return node;
}
}
/**
* 符号以空格分隔,节点上的值可以大于9
* @param str
* @return
*/
public static TreeNode stringToTreeWith(String str,String separator){
k2=0;
TreeNode root=null;
String[] arr=str.trim().split(separator);
root=createTreeWith(root,arr);
return root;
}
private static TreeNode createTreeWith(TreeNode node,String[] arr){
String v=arr[k2++];
if(v.equals("#")) return null;
else{
node=new TreeNode(Integer.valueOf(v));
node.left=createTreeWith(node.left,arr);
node.right=createTreeWith(node.right,arr);
return node;
}
}
/**
* 先序遍历
* @param root
*/
public static void travser(TreeNode root){
if(root!=null){
System.out.print(root.val+" ");
travser(root.left);
travser(root.right);
}
}
/**
* 中序遍历
* @param root
*/
public static void travser2(TreeNode root){
if(root!=null){
travser2(root.left);
System.out.print(root.val+" ");
travser2(root.right);
}
}
/**
* 后序遍历
* @param root
*/
public static void travser3(TreeNode root){
if(root!=null){
travser3(root.left);
travser3(root.right);
System.out.print(root.val+" ");
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.