PAT: Root of AVL Tree (25),Go语言
u013564276 · · 1944 次点击 · · 开始浏览An AVL tree is a self-balancing binary search tree. In an AVLtree, the heights of the two child subtrees of any node differ byat most one; if at any time they differ by more than one,rebalancing is done to restore this property. Figures 1-4illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to tell theroot of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the firstline contains a positive integer N (<=20) which is the totalnumber of keys to be inserted. Then N distinct integer keys aregiven in the next line. All the numbers in a line are separated bya space.
Output Specification:
For each test case, print ythe root of the resulting AVL tree inone line.
Sample Input 1:5 88 70 61 96 120Sample Output 1:
70Sample Input 2:
7 88 70 61 96 120 90 65Sample Output 2:
88 --------------------------------------------------------------------------------
代码参考了这位博主http://blog.csdn.net/tiantangrenjian/article/details/12891091,并将其改写为了Go语言的版本
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
An AVL tree is a self-balancing binary search tree. In an AVLtree, the heights of the two child subtrees of any node differ byat most one; if at any time they differ by more than one,rebalancing is done to restore this property. Figures 1-4illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to tell theroot of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the firstline contains a positive integer N (<=20) which is the totalnumber of keys to be inserted. Then N distinct integer keys aregiven in the next line. All the numbers in a line are separated bya space.
Output Specification:
For each test case, print ythe root of the resulting AVL tree inone line.
Sample Input 1:5 88 70 61 96 120Sample Output 1:
70Sample Input 2:
7 88 70 61 96 120 90 65Sample Output 2:
88 --------------------------------------------------------------------------------
代码参考了这位博主http://blog.csdn.net/tiantangrenjian/article/details/12891091,并将其改写为了Go语言的版本
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117