offer收割机

  1. 关于Git
    1. 基础设置
  2. 链表
  3. 二叉树
    1. 二叉树的遍历
      1. 前序遍历
      2. 中序遍历
      3. 后序遍历
      4. 层序遍历
        1. 二叉树的序列化与反序列化

关于Git

基础设置

安装成功后进行设置

  • 关于账户
1
2
3
4
5
6
7
git config --global user.name "your name"//设置用户名
git config --global user.email "your email"//设置邮箱
进行设置检查
git config --global -l
或者
git config --global user.name
git config --global user.email

之后要进行github账户的注册登录,这是本地仓库要上传共享的地方,也是与广大开发者交流的地方

1
2
3
4
5
6
7
设置github的SSH Key:在本地开发时将代码上传到远程仓库,通过公私密钥的方式确保安全性
ssh-keygen -t rsa -C "your email@example.com"//与github账户邮箱一致
按回车(不设置密码按三次回车即可)
通过命令获取生成的公钥后复制到github上去
cat ~/.ssh/id_rsa.pub
通过以下命令检查
ssh -T git@github.com
  • 关于安全性
1
s

链表

二叉树

二叉树的遍历

  • 分为前序、中序和后序以及深度优先、广度优先遍历等五种方式
  • 删除节点时,按照后序遍历顺序进行,即删除左右节点后再删除节点本身
  • 使用中序遍历可以得到一个二叉搜索树的递增序列
  • 使用后续可以解析数学表达式的后缀表示,处理表达式时结合使用栈,每遇到一个操作符可以从栈中弹出栈顶两个元素计算并返回结果到栈中

前序遍历

按照根节点,左子树,右子树的顺序遍历

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
***************************************
递归实现 N N
***************************************
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<Integer>res=new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root==null)return res;
res.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return res;
}
}
***************************************
迭代——栈实现N N
***************************************
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
Stack<TreeNode>s=new Stack<>();
while(!s.isEmpty()||root!=null){
while(root!=null){
res.add(root.val);
s.push(root);
root=root.left;
}
root=s.pop();
root=root.right;
}
return res;
}
}
***************************************
迭代——Mirrors实现
***************************************
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
if(root==null)return res;
TreeNode tmp1=root,tmp2=null;
while(tmp1!=null){
tmp2=tmp1.left;
if(tmp2==null){
res.add(tmp1.val);
tmp1=tmp1.right;
}else{
while(tmp2.right!=null&&tmp2.right!=tmp1){
tmp2=tmp2.right;
}
if(tmp2.right==null){
tmp2.right=tmp1;
res.add(tmp1.val);
tmp1=tmp1.left;
}else{
tmp2.right=null;
tmp1=tmp1.right;
}
}
}
return res;
}
}

中序遍历

按照左子树,根节点,右子树的顺序遍历

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
***************************************
递归实现 N N
***************************************
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<Integer>res=new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root==null)return res;
inorderTraversal(root.left);
res.add(root.val);
inorderTraversal(root.right);
return res;
}
}
***************************************
迭代——栈实现N N
***************************************
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
Stack<TreeNode>s=new Stack<>();
while(!s.isEmpty()||root!=null){
while(root!=null){
s.push(root);
root=root.left;
}
root=s.pop();
res.add(root.val);
root=root.right;

}
return res;
}
}
***************************************
迭代——Mirrors实现
***************************************
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
if(root==null)return res;
TreeNode t1=root,t2=null;
while(t1!=null){
t2=t1.left;
if(t2==null){
res.add(t1.val);
t1=t1.right;
}else{
while(t2.right!=null&&t2.right!=t1){
t2=t2.right;
}
if(t2.right==null){
t2.right=t1;
t1=t1.left;
}else{
t2.right=null;
res.add(t1.val);
t1=t1.right;
}
}
}
return res;
}
}

后序遍历

按照左子树,右子树,根节点的顺序遍历

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
101
102
103
104
105
***************************************
递归实现 N N
***************************************
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<Integer>res=new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root==null)return res;
postorderTraversal(root.left);
postorderTraversal(root.right);
res.add(root.val);
return res;
}
}
***************************************
迭代——栈实现N N
***************************************
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
Stack<TreeNode>s=new Stack<>();
TreeNode pre=null;
s.push(root);
while(!s.isEmpty()||root!=null){
while(root!=null){
s.push(root);
root=root.left;
}
root=s.pop();
if(root.right==null||root.right==pre){
res.add(root.val);
pre=root;
root=null;
}else{
s.push(root);
root=root.right;
}
}
return res;
}
}
***************************************
迭代——栈实现N N根据前序改变
***************************************
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
Stack<TreeNode>s=new Stack<>();
while(!s.isEmpty()||root!=null){
while(root!=null){
res.add(root.val);
s.push(root);
root=root.right;
}
root=s.pop();
root=root.left;
}
Collections.reverse(res);
return res;
}
}
***************************************
迭代——Mirrors实现
***************************************
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer>res=new ArrayList<>();
if(root==null)return res;
TreeNode tmp1=root,tmp2=null;
while(tmp1!=null){
tmp2=tmp1.left;
if(tmp2==null){
res.add(tmp1.val);
tmp1=tmp1.right;
}else{
while(tmp2.right!=null&&tmp2.right!=tmp1){
tmp2=tmp2.right;
}
if(tmp2.right==null){
tmp2.right=tmp1;
res.add(tmp1.val);
tmp1=tmp1.left;
}else{
tmp2.right=null;
tmp1=tmp1.right;
}
}
}
return res;
}
}

层序遍历

  • BFS

    广度优先搜索,顾名思义就是逐层遍历,根据题目需要加入数据,常用的数据结构是队列的迭代实现

  • DFS

    逐层的遍历实现,用深度优先也可以实现,在DFS的时候引入层数即可,常用递归实现,数据结构为栈

二叉树的序列化与反序列化
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
BFS方法
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {

// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root==null)return "";
Queue<TreeNode>q=new LinkedList<>();
StringBuilder sb=new StringBuilder();
q.add(root);
sb.append("[");
while(!q.isEmpty()){
TreeNode tmp=q.poll();
if(tmp!=null){
sb.append(""+tmp.val);
q.add(tmp.left);
q.add(tmp.right);
}else{
sb.append("null");
}
sb.append(',');
}
sb.append("]");
return sb.toString();
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data=="")return null;
Queue<TreeNode>q=new LinkedList<>();
String[] res=data.substring(1,data.length()-1).split(",");
int i=1;
TreeNode root=new TreeNode(Integer.parseInt(res[0]));
q.add(root);
while(!q.isEmpty()){
TreeNode tmp=q.poll();
if(!"null".equals(res[i])){
tmp.left=new TreeNode(Integer.parseInt(res[i]));
q.add(tmp.left);
}
i++;
if(!"null".equals(res[i])){
tmp.right=new TreeNode(Integer.parseInt(res[i]));
q.add(tmp.right);
}
i++;
}
return root;
}
}

DFS方法
public class Codec {

// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root==null)return null;
return root.val+","+serialize(root.left)+","+serialize(root.right);
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if(data=="")return null;
Queue<String>q=new LinkedList<>(Arrays.asList(data.split(",")));
return dfs(q);
}
public TreeNode dfs(Queue<String>q){
String val=q.poll();
if(val.equals("null"))
return null;
TreeNode root=new TreeNode(Integer.parseInt(val));
root.left=dfs(q);
root.right=dfs(q);
return root;
}
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1293102962@qq.com

×

喜欢就点赞,疼爱就打赏

菜鸟窝 github