File tree Expand file tree Collapse file tree 1 file changed +38
-2
lines changed Expand file tree Collapse file tree 1 file changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -132,9 +132,45 @@ https://github.com/CarpenterLee/JCFInternals/blob/049c84bb65a3114ba4b8355d83c490
132
132
133
133
#### 二分查找法
134
134
135
- #### 二叉树遍历
135
+ 在有序表中,通过不断的二分判断mid与目标是否一致,并缩小目标所在区间。
136
136
137
- ### 4.. 二分搜索树
137
+ ** 代码实现**
138
+
139
+ ``` java
140
+ // 非递归实现
141
+ private static int search(int [] data, int l, int r, int target) {
142
+ int mid;
143
+ while (l < r) {
144
+ mid = (l + r) / 2 ;
145
+ if (data[mid] == target) {
146
+ return mid;
147
+ } else if (data[mid] < target) {
148
+ l = mid + 1 ;
149
+ } else {
150
+ r = mid;
151
+ }
152
+ }
153
+ return - 1 ;
154
+ }
155
+ // 递归实现
156
+ private static int searchDfs(int [] data, int l, int r, int target) {
157
+ if (l >= r) {
158
+ return - 1 ;
159
+ }
160
+ int mid = (l + r) / 2 ;
161
+ if (target == data[mid]) {
162
+ return mid;
163
+ } else if (target > data[mid]) {
164
+ return searchDfs(data, mid + 1 , r, target);
165
+ } else {
166
+ return searchDfs(data, l, mid, target);
167
+ }
168
+ }
169
+ ```
170
+
171
+ #####
172
+
173
+ ### 4.. 二叉树遍历
138
174
139
175
#### 深度优先遍历(前序、中序、后序遍历)
140
176
You can’t perform that action at this time.
0 commit comments