Skip to content

Commit fa1760a

Browse files
committed
28
1 parent 39b285b commit fa1760a

File tree

5 files changed

+173
-44
lines changed

5 files changed

+173
-44
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
对于第二种,我们好判断,但是对于第一种方式,无法快速的找到其左子树的最右孩子,因此我们链接的时候需要保存其前驱节点,我们称之为lastNode节点
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type TreeNode struct {
8+
Val int
9+
Left *TreeNode
10+
Right *TreeNode
11+
}
12+
13+
func TreeToList(root *TreeNode) (*TreeNode, *TreeNode) {
14+
head, tail := root, root
15+
if root == nil { return head, tail }
16+
if root.Left == nil && root.Right == nil {
17+
head.Left, tail.Right = root, root
18+
return head, tail
19+
}
20+
if root.Left != nil {
21+
leftHead, leftTail := TreeToList(root.Left)
22+
head = leftHead
23+
root.Left = leftTail
24+
leftTail.Right = root
25+
}
26+
if root.Right != nil {
27+
rightHead, rightTail := TreeToList(root.Right)
28+
tail = rightTail
29+
root.Right = rightHead
30+
rightHead.Left = root
31+
}
32+
head.Left, tail.Right = head, tail
33+
return head, tail
34+
}
35+
36+
37+
38+
func printRight(root *TreeNode){
39+
for root.Right != root {
40+
fmt.Printf("%d ", root.Val)
41+
root = root.Right
42+
}
43+
fmt.Printf("%d ", root.Val)
44+
}
45+
46+
func printLeft(root *TreeNode){
47+
for root.Left != root {
48+
fmt.Printf("%d ", root.Val)
49+
root = root.Left
50+
}
51+
fmt.Printf("%d ", root.Val)
52+
}
53+
54+
func main() {
55+
//test
56+
root := &TreeNode{5, nil, nil}
57+
l11 := &TreeNode{3, nil, nil}
58+
l12 := &TreeNode{8, nil, nil}
59+
l21 := &TreeNode{1, nil, nil}
60+
l22 := &TreeNode{4, nil, nil}
61+
l23 := &TreeNode{6, nil, nil}
62+
l24 := &TreeNode{9, nil, nil}
63+
root.Left, root.Right = l11, l12
64+
l11.Left, l11.Right = l21, l22
65+
l12.Left, l12.Right = l23, l24
66+
head, tail := TreeToList(root)
67+
printLeft(tail)
68+
fmt.Printf("\n")
69+
printRight(head)
70+
fmt.Printf("\n")
71+
72+
fmt.Printf("\n")
73+
74+
root = &TreeNode{3, &TreeNode{1, nil, nil}, &TreeNode{4, nil, nil}}
75+
head, tail = TreeToList(root)
76+
printLeft(tail)
77+
fmt.Printf("\n")
78+
printRight(head)
79+
fmt.Printf("\n")
80+
81+
}

028-字符串的排列/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#题意
2+
3+
题目描述
4+
5+
输入一个字符串,按字典序打印出该字符串中字符的所有排列
6+
7+
例如输入字符串abc,
8+
9+
则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
10+
11+
结果请按字母顺序输出。
12+
13+
注意 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母
14+
15+
样例输入
16+
17+
>abc
18+
>BCA
19+
20+
样例输出
21+
22+
>abc acb bac bca cab cba
23+
>ABC ACB BAC BCA CAB CBA
24+
25+
# 分析
26+
27+
类似 [LeetCode 47. Permutations II](https://leetcode.com/problems/permutations-ii/)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func stringPermutation(str string) []string {
8+
var res []string
9+
s := []byte(str)
10+
length := len(s)
11+
var dfs func(idx int)
12+
dfs = func(idx int){
13+
if idx == length {
14+
str := string(s)
15+
res = append(res, str)
16+
return
17+
}
18+
m := make(map[byte]bool)
19+
for i:=idx; i<length; i++{
20+
if _,ok := m[s[i]]; ok { continue }
21+
m[s[i]] = true
22+
s[idx], s[i] = s[i], s[idx]
23+
dfs(idx+1)
24+
s[i], s[idx] = s[idx], s[i]
25+
}
26+
}
27+
dfs(0)
28+
return res
29+
}
30+
31+
func main() {
32+
//test
33+
s := "abc"
34+
b := "ABC"
35+
d := "aab"
36+
fmt.Println(s, " Permutation: ", stringPermutation(s))
37+
fmt.Println(b, " Permutation: ", stringPermutation(b))
38+
fmt.Println(d, " Permutation: ", stringPermutation(d))
39+
40+
}

bash.sh

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)