网站维护一般怎么做,全国信用企业信息公示系统查询,瀑布流 网站 php 源码,新网seo关键词优化教程题目 普通的伞在二维平面世界中#xff0c;左右两侧均有一条边#xff0c;而两侧伞边最下面各有一个伞坠子#xff0c;雨滴落到伞面#xff0c;逐步流到伞坠处#xff0c;会将伞坠的信息携带并落到地面#xff0c;随着日积月累#xff0c;地面会呈现伞坠的信息。 1、为了…题目 普通的伞在二维平面世界中左右两侧均有一条边而两侧伞边最下面各有一个伞坠子雨滴落到伞面逐步流到伞坠处会将伞坠的信息携带并落到地面随着日积月累地面会呈现伞坠的信息。 1、为了模拟伞状雨滴效应用二叉树来模拟二维平面伞(如下图所示)现在输入一串正整数数组序列(不含0数组成员至少是1个)若此数组序列是二叉搜索树的前序遍历的结果那么请输出一个返回值1否则输出0. 2、同时请将此序列构成的伞状效应携带到地面的数字信息输出来(左边伞坠信息右边伞坠信息详细参考示例图地面上数字)若此树不存在左或右扇坠则对应位置返回0。同时若非二叉排序树那么左右伞坠信息也返回0. 输入描述: 1个通过空格分割的整数序列字符串数组不含0数组成员至少1个输入的数组的任意两个数字都互不相同最多1000个正整数正整数值范围1~65535 输出描述: 输出如下三个值以空格分隔:是否二叉排序树左侧地面呈现的伞坠数字值右侧地面呈现的伞坠数字值.若是二叉排序树则输出1否则输出0(其左右伞坠值也直接赋值0)。 若不存存在左侧或者右侧伞坠值那么对应伞坠值直接赋值0。 示例1 输入: 8 3 1 6 4 7 10 14 13 输出: 1 1 13 说明: 1 表示是二叉搜索树前序遍历结果1表示左侧地面呈现的伞坠数字值13表示右侧地面呈现的伞坠数字值 思路 本题化解为两个问题 根据前序遍历数组中左右判断其是否能构成二叉搜索树左节点中间节点右节点 以示例数据为例8 3 1 6 4 7 10 14 13 根据前序遍历及二叉搜索树的特点第一个数8应该为根节点其后面小于8的所有数3 1 6 4 7在8的左边大于8的所有数10 14 13 在8的右边。问题化解为两个字数组是否为二叉搜索树用同样的办法递归判断即可。 设计dfs方法为dfs(nums,start,end)当前节点为nums[start]设istart1,不断右移i先找到第一个比当前节点大的数在索引【start1,i-1】就是左节点的部分继续右移i直到i越界或者nums[i]小于当前值。如果最后i是越界的iend1说明后面的数都比nums[start]大满足二叉搜索树的规律继续递归两个子数组【start1,i-1】和【i,end】递归中止条件是startend即只有一个数字返回true否则不满足直接返回false 求左右伞坠关键在于理解左右伞坠的定义题目并没有明确说明我的理解如下 如果不是二叉搜索树左右伞坠直接置0如果根节点没有左子树那么左伞坠为0如果没有右子树那么右伞坠为0寻找左伞坠的方法优先寻找左子树当左节点为空时再找右节点寻找右伞坠的方法优先寻找右子树当右节点为空时再找左节点 比如示例数据8 3 1 6 4 7 10 14 13输出1 1 13 删除一个节点8 3 6 4 7 10 14 13输出1 4 13 只要左子树8 3 1 6 4 7输出1 1 0 只要右子树8 10 14 13输出1 0 13 只要根节点8输出:1 0 0 题解
package hwod;import java.util.Arrays;
import java.util.Scanner;public class UmbrellaRainDrop {public static void main(String[] args) {Scanner sc new Scanner(System.in);int[] nums Arrays.stream(sc.nextLine().split( )).mapToInt(Integer::parseInt).toArray();int[] res umbrellaRainDrop(nums);for (int i 0; i res.length; i) {if (i ! 0) System.out.print( );System.out.print(res[i]);}System.out.println();}private static int[] umbrellaRainDrop(int[] nums) {boolean isSearchTree recur(nums, 0, nums.length - 1);if (!isSearchTree) return new int[]{0, 0, 0};int leftNum findLastNum(nums, 0, nums.length - 1, true);int rightNum findLastNum(nums, 0, nums.length - 1, false);return new int[]{1, leftNum, rightNum};}private static int findLastNum(int[] nums, int start, int end, boolean isLeft) {if (start end) return start 0 ? 0 : nums[start];int i start 1;while (i end nums[i] nums[start]) i;//处理没有左右伞坠的情况if (isLeft i start 1 start 0) return 0;if (!isLeft i end 1 start 0) return 0;if (isLeft) {//先找左边如果没有左节点就找右节点return i start 1 ? findLastNum(nums, i, end, isLeft) : findLastNum(nums, start 1, i - 1, isLeft);} else {//先找右边如果没有右节点就找左节点return i end 1 ? findLastNum(nums, start 1, i - 1, isLeft) : findLastNum(nums, i, end, isLeft);}}private static boolean recur(int[] nums, int start, int end) {if (start end) {return true;}int i start 1;while (i end nums[i] nums[start]) i;int tmp i;while (i end nums[i] nums[start]) i;if (i ! end 1) return false;return recur(nums, start 1, tmp - 1) recur(nums, tmp, end);}
}
推荐
如果你对本系列的其他题目感兴趣可以参考华为OD机试真题及题解JAVA查看当前专栏更新的所有题目。 文章转载自: http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.blqsr.cn.gov.cn.blqsr.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.cctgww.cn.gov.cn.cctgww.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.ztqj.cn.gov.cn.ztqj.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.jhswp.cn.gov.cn.jhswp.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.a3e2r.com.gov.cn.a3e2r.com http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.lszjq.cn.gov.cn.lszjq.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.gxhqt.cn.gov.cn.gxhqt.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.zxgzp.cn.gov.cn.zxgzp.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.dswtz.cn.gov.cn.dswtz.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.trsmb.cn.gov.cn.trsmb.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.lmmkf.cn.gov.cn.lmmkf.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.lftpl.cn.gov.cn.lftpl.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.nsrtvu.com.gov.cn.nsrtvu.com http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn