leetcode - 04 树专题 114~106~96~863~剑指Offer 27~257~108~617~ 剑指 Offer 07 重建二叉树~99
admin
2024-02-16 23:29:40
0

文章目录

          • 114. 二叉树展开为链表
          • 106. 从中序与后序遍历序列构造二叉树
          • 96. 不同的二叉搜索树
          • 863. 二叉树中所有距离为 K 的结点
          • 剑指 Offer 27. 二叉树的镜像
          • 257. 二叉树的所有路径
          • 108. 将有序数组转换为二叉搜索树
          • 617. 合并二叉树
          • 剑指 Offer 07. 重建二叉树
          • 99. 恢复二叉搜索树

114. 二叉树展开为链表
class Solution {public void flatten(TreeNode root) {while (root!=null){// 重复处理TreeNode pNode = root.left;if(pNode != null){// 找到root节点左子树的右子树的最右侧节点while (pNode.right!=null){pNode = pNode.right;}// pNode的右子节点指向root的右子节点pNode.right = root.right;// root的右子节点执行root的左子节点root.right = root.left;// root的左子节点置为nullroot.left = null;}root = root.right;}}
}
106. 从中序与后序遍历序列构造二叉树
class Solution {HashMap map = new HashMap<>();public TreeNode buildTree(int[] inorder, int[] postorder) {int inleft = 0;int inrignt = inorder.length-1;int postleft = 0;int postright = postorder.length-1;for (int i=0;imap.put(inorder[i],i);}return rebuild(inleft,inrignt,postleft,postright,postorder,inorder);}private TreeNode rebuild(int inleft, int inrignt,  int postleft, int postright, int[] postorder, int[] inorder) {if(inleft>inrignt || postleft>postright){return null;}int rootValue = postorder[postright];TreeNode root = new TreeNode(rootValue);int index = map.get(rootValue);root.left = rebuild(inleft,index-1,postleft,postleft+index-inleft-1,postorder,inorder);root.right = rebuild(index+1,inrignt,postleft+index-inleft,postright-1,postorder,inorder);return root;}
}
96. 不同的二叉搜索树
class Solution {public int numTrees(int n) {// G(n) = G(0)*G(n-1) + G(1)*G(n-2) + ... +G(n-1)*G(0)// 动态规划int[] dp = new int[n+1];dp[0] = 1;dp[1] = 1;for(int i=2;i<=n;i++){for(int j=1;j<=i;j++){// G(n) = G(0)*G(n-1) + G(1)*G(n-2) + ... + G(n-1)*G(0)//dp[2] = dp[0]*dp[1] +  dp[1]*dp[0]dp[i] += dp[j-1]*dp[i-j];}}return dp[n];}
}
863. 二叉树中所有距离为 K 的结点

class Solution {Map map = new HashMap<>();List res = new ArrayList<>();public List distanceK(TreeNode root, TreeNode target, int k) {// 从root出发dfs记录每个节点的父节点findParant(root);// 从target出发dfs寻找所有深度为k的节点TreeNode from = null;int depth = 0;findRes(target,from,depth,k);return res;}private void findRes(TreeNode node, TreeNode from, int depth, int k) {if(node==null){return;}if(depth==k){res.add(node.val);return;}if(node.left!=from){findRes(node.left,node,depth+1,k);}if(node.right!=from){findRes(node.right,node,depth+1,k);}if(map.get(node.val) != from){findRes(map.get(node.val),node,depth+1,k);}}private void findParant(TreeNode node) {if(node.left!=null){map.put(node.left.val,node);findParant(node.left);}if(node.right!=null){map.put(node.right.val,node);findParant(node.right);}}
}
剑指 Offer 27. 二叉树的镜像
class Solution {public TreeNode mirrorTree(TreeNode root) {if(root==null){return null;}TreeNode temp = root.left;root.left = root.right;root.right = temp;mirrorTree(root.left);mirrorTree(root.right);return root;}
}
257. 二叉树的所有路径

方法1:

class Solution {public List binaryTreePaths(TreeNode root) {List res = new ArrayList<>();dfs(root,"",res);return res;}private void dfs(TreeNode root, String path, List res) {if(root==null){return;}// 到达根节点,将路径添加到resif(root.left==null && root.right==null){res.add(path+root.val);return;}// 不是叶子节点,分别遍历左右节点dfs(root.left,path+root.val+"->",res);dfs(root.right,path+root.val+"->",res);}
}

方法2:

class Solution {public List binaryTreePaths(TreeNode root) {List res = new ArrayList<>();Stack stack = new Stack<>();// 将节点的入栈stack.push(root);// 将节点路径入栈stack.push(root.val+"");while (!stack.isEmpty()){String path = (String)stack.pop();TreeNode node = (TreeNode)stack.pop();if(node.left==null && node.right==null){// 说明是叶子节点res.add(path);}if(node.left!=null){stack.push(node.left);stack.push(path+"->"+node.left.val);}if(node.right!=null){stack.push(node.right);stack.push(path+"->"+node.right.val);}}return res;}
}
 
108. 将有序数组转换为二叉搜索树
class Solution {public TreeNode sortedArrayToBST(int[] nums) {if(nums.length==0){return null;}int left  = 0;int right = nums.length-1;return dfs(nums,left,right);}private TreeNode dfs(int[] nums, int left, int right) {if(left>right){return null;}int mid = (left+right)/2;TreeNode root = new TreeNode(nums[mid]);root.left = dfs(nums,left,mid-1);root.right = dfs(nums,mid+1,right);return root;}
}
617. 合并二叉树

方法1:

class Solution {public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {if(root1==null || root2==null){return root1==null ? root2 : root1;}return dfs(root1,root2);}private TreeNode dfs(TreeNode root1, TreeNode root2) {if(root1==null || root2==null){return root1==null ? root2 : root1;}root1.val = root1.val+root2.val;root1.left = dfs(root1.left,root2.left);root1.right = dfs(root1.right,root2.right);return root1;}
}
剑指 Offer 07. 重建二叉树
class Solution {HashMap map = new HashMap<>()public TreeNode buildTree(int[] preorder, int[] inorder) {int preLeft = 0;int preRight = preorder.length-1;int inLeft= 0;int inRight = inorder.length-1;for(int i=0;imap.put(inorder[i],i);}return dfs(preLeft,preRight,preorder,inLeft,inRight,inorder,map);}private TreeNode dfs(int preLeft, int preRight, int[] preorder, int inLeft, int inRight, int[] inorder, HashMap map) {if(inLeft>inRight || preLeft>preRight){return null;}int rootValue = preorder[preLeft];TreeNode root = new TreeNode(rootValue);Integer index = map.get(rootValue);root.left = dfs(preLeft+1,preLeft+index-inLeft,preorder,inLeft,index-1,inorder,map);root.right = dfs(preLeft+index-inLeft+1,preRight,preorder,index+1,inRight,inorder,map);return root;}
}
99. 恢复二叉搜索树
class Solution {public void recoverTree(TreeNode root) {// 获取中序遍历结果List list = new ArrayList<>();dfs(root,list);// 找出中序遍历中错误顺序的两个数TreeNode x = null;TreeNode y = null;for(int i = 0;iif(list.get(i).val>list.get(i+1).val){y = list.get(i+1);if(x==null){x = list.get(i);}}}// 交换x和yif(x!=null && y!=null){int temp = x.val;x.val = y.val;y.val = temp;}}private void dfs(TreeNode root, List list) {if(root==null){return;}dfs(root.left,list);list.add(root);dfs(root.right,list);}
}

方法2:

相关内容

热门资讯

斗金订购APP贵金属期货投资被...   斗金订购APP的投资者被广告宣传给诱导,注册就送什么现金,然后充值返现金卷等等这些宣传方式,都是...
哈易购APP非法期货交易欺骗投...   哈易购APP宣传可做白银铂金贵金属订购交易,但实际上并没有取得相关交易资质!哈易购APP本质上就...
消息称百度旗下昆仑芯瞄准500... 6 月 29 日消息,据《The Information》昨日援引知情人士消息,百度旗下 AI 芯片...
打造夏日消费新场景 第35届北... 北京商报讯(记者 翟枫瑞)6月29日消息,第35届北京国际燕京啤酒文化节新闻发布会在京举行。本届啤酒...
社保基金持仓数据出炉,一季度增... 最近各大上市公司一季度财报都公开了,咱们国家社保基金的持仓数据也全部曝光。目前社保拿着比亚迪价值44...
36氪首发 | 海思、中兴团队... 作者 | 乔钰杰 编辑 | 袁斯来 硬氪获悉,广州宸思通讯科技有限公司(以下简称“宸思科技”)近日完...
两天蒸发47亿市值!一纸税务通... 一纸税务通知书,能让一家百亿龙头两天蒸发47亿市值。 6月22日,北大荒(600598.SH)公告称...
SK海力士将投资1100万亿韩... SK集团会长崔泰源6月29日在韩国“三大重大计划”发布会上宣布,公司将投资1100万亿韩元扩大半导体...
两只A股,终止上市! 两家A股公司,即将摘牌。 6月29日,退市沪科(600608.SH)公告称,上海证券交易所将在202...
原创 M... 一家成立近十年的自动驾驶公司,在IPO时吸引了14家基石投资者认购近一半的发行股份,其中不乏奔驰、比...
基金忠言|国寿安保滤镜碎,三年... 图片来源:视觉中国 蓝鲸新闻6月29日讯(记者 祁和忠)保险系基金公司国寿安保总经理换人了。 6月2...
三星电机计划加码玻璃基板!相关... 6月29日,玻璃基板概念股午后有所回升, 华工科技(000988.SZ)逼近涨停, 彩虹股份(600...
拉萨海关持续壮大外贸经营主体 ...   新华网拉萨6月28日电(记者蒋梦辰)近日,记者从拉萨海关获悉,今年前5个月,西藏有进出口实绩的外...
机构:二季报临近,医药生物板块... 6月29日,华源证券发布了一篇医药生物行业的研究报告,报告指出,业绩期临近,产业链景气度有望再次迎来...
每日收评科创50放量涨超4.5... 财联社6月29日讯,三大指数全线收红,创业板指探底回升,科创50指数大涨4.61%。沪深两市成交额3...
6月多地土拍结构性升温:深圳单... 进入2026年6月,不少城市核心区地块集中诞生高溢价宗地,热度突出的城市包含深圳、杭州、长沙。 其中...
业绩炸裂!盛达资源半年预盈3.... 6月29日,贵金属矿山龙头盛达资源(000603.SZ)发布 2026 年半年度业绩预告,上半年业绩...
A股午后拉升三大股指收涨:半导... A股三大股指6月29日开盘涨跌互现。早盘沪强深弱,创指一度跌超2%。半导体午后拉升,带动两市上涨,沪...
原创 空... 前言 大家好,我是老金。 这几天,两幅极度割裂的画面放在一起,把我看笑了。 一边是在持续的热浪下,欧...