专门做汽车配件的网站,帮企商城源码,太原新站优化,爱站网长尾挖掘工具力扣爆刷第142天之二叉树五连刷#xff08;构造树、搜索树#xff09; 文章目录 力扣爆刷第142天之二叉树五连刷#xff08;构造树、搜索树#xff09;一、106. 从中序与后序遍历序列构造二叉树二、654. 最大二叉树三、617. 合并二叉树四、700. 二叉搜索树中的搜索五、98. …力扣爆刷第142天之二叉树五连刷构造树、搜索树 文章目录 力扣爆刷第142天之二叉树五连刷构造树、搜索树一、106. 从中序与后序遍历序列构造二叉树二、654. 最大二叉树三、617. 合并二叉树四、700. 二叉搜索树中的搜索五、98. 验证二叉搜索树 一、106. 从中序与后序遍历序列构造二叉树
题目链接https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 思路首先把中序遍历的key和value用map记录下来节省通过后序定位根节点的时间然后不断的用父节点划分左右子数组。
class Solution {MapInteger, Integer map new HashMap();public TreeNode buildTree(int[] inorder, int[] postorder) {for(int i 0; i inorder.length; i) {map.put(inorder[i], i);}return traverse(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);}TreeNode traverse(int[] inorder, int[] postorder, int leftIn, int rightIn, int leftPo, int rightPo) {if(leftIn rightIn) return null;int mid map.get(postorder[rightPo]);TreeNode root new TreeNode(postorder[rightPo]);root.left traverse(inorder, postorder, leftIn, mid-1, leftPo, leftPomid-leftIn-1);root.right traverse(inorder, postorder, mid1, rightIn, leftPomid-leftIn, rightPo-1);return root;}}二、654. 最大二叉树
题目链接https://leetcode.cn/problems/maximum-binary-tree/description/ 思路也是前序遍历构建二叉树在每一次指定区间的内通过比较获取最大值然后通过最大值划分左右子数组。
class Solution {public TreeNode constructMaximumBinaryTree(int[] nums) {return buildTree(nums, 0, nums.length-1);}TreeNode buildTree(int[] nums, int left, int right) {if(left right) return null;int max nums[left], mid left;for(int i left; i right; i) {if(nums[i] max) {max nums[i];mid i;}}TreeNode root new TreeNode(max);root.left buildTree(nums, left, mid-1);root.right buildTree(nums, mid1, right);return root;}
}三、617. 合并二叉树
题目链接https://leetcode.cn/problems/merge-two-binary-trees/description/ 思路合并二叉树其实就是遍历其中一棵树然后把另外一颗树连接到这棵树上。
class Solution {public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {if(root1 null root2 null) {return null;}if(root1 null) return root2;if(root2 null) return root1;root1.val root2.val;root1.left mergeTrees(root1.left, root2.left);root1.right mergeTrees(root1.right, root2.right);return root1;}}四、700. 二叉搜索树中的搜索
题目链接https://leetcode.cn/problems/search-in-a-binary-search-tree/description/ 思路利用二叉搜索树的特性从上往下进行搜索相等返回小于去左子树大于去右子树。
class Solution {public TreeNode searchBST(TreeNode root, int val) {if(root null) return null;if(root.val val) {return root;}else if(root.val val) {return searchBST(root.left, val);}else{return searchBST(root.right, val);}}}五、98. 验证二叉搜索树
题目链接https://leetcode.cn/problems/validate-binary-search-tree/description/ 思路要想验证是不是二叉搜索树直接利用二叉搜索树的特性中序单调遍历递增只要非单调递增即不是。
class Solution {boolean flag true;TreeNode p null;public boolean isValidBST(TreeNode root) {traverse(root);return flag;}void traverse(TreeNode root) {if(root null) return ;traverse(root.left);if(p ! null) {if(p.val root.val) {flag false;return ;}}p root;traverse(root.right);}
}
文章转载自: http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.ckxd.cn.gov.cn.ckxd.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.lfbzg.cn.gov.cn.lfbzg.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.gbfck.cn.gov.cn.gbfck.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.c7500.cn.gov.cn.c7500.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.nptls.cn.gov.cn.nptls.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.vaqmq.cn.gov.cn.vaqmq.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.zrhhb.cn.gov.cn.zrhhb.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.ttxnj.cn.gov.cn.ttxnj.cn http://www.morning.lhwlp.cn.gov.cn.lhwlp.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.jlgjn.cn.gov.cn.jlgjn.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.nyzmm.cn.gov.cn.nyzmm.cn http://www.morning.rrwgh.cn.gov.cn.rrwgh.cn