当前位置: 首页 > news >正文

如何做网站反链百度一下首页百度一下知道

如何做网站反链,百度一下首页百度一下知道,wordpress如何添加301规则,网站建设 网页制作二叉树展开为链表 给你二叉树的根结点 root ,请你将它展开为一个单链表: 展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。展开后的单链表应该与二叉树 先序遍历 顺序相同…

二叉树展开为链表

给你二叉树的根结点 root ,请你将它展开为一个单链表:

  • 展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
  • 展开后的单链表应该与二叉树 先序遍历 顺序相同。

示例1:
在这里插入图片描述
输入:root = [1,2,5,3,4,null,6]
输出:[1,null,2,null,3,null,4,null,5,null,6]

解题思路

展开二叉树为单链表可以使用前序遍历的方式来实现。

  • 1、对于当前节点,首先将其左子树展开为单链表,并将左子树的最右节点连接到当前节点的右子树上。
  • 2、然后将当前节点的右子树展开为单链表。
  • 3、如果左子树不为空,将当前节点的右子树设为展开后的左子树;否则,将左子树设为右子树。

Java实现

public class FlattenBinaryTreeToLinkedList {static class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int val) {this.val = val;}}public void flatten(TreeNode root) {if (root == null) {return;}flattenHelper(root);}private TreeNode flattenHelper(TreeNode node) {if (node == null) {return null;}//    1//   / \//  2   5// / \   \//3   4   6// 把2的右子树4放到2的左子树3的右子树上//      1//     / \//    2   5//   / \   \//  3   4   6//   \//    4//把2左子树3-4移到右子树下,把2的左子树置空//      1//     / \//    2   5//     \   \//      3   6//       \//        4//递归,把1的右子树5-6移到1的左子树2-3-4的右子树下//      1//     / \//    2   5//     \   \//      3   6//       \//        4//         \//          5//           \//            6//把1的左子树2-3-4-5-6替换到右子树上,把1的左子树置空//      1//       \//        2//         \//          3//           \//            4//             \//              5//               \//                6//链表符合前序遍历了,并且左子树全为null//下面是具体代码实现TreeNode leftTail = flattenHelper(node.left);TreeNode rightTail = flattenHelper(node.right);if (leftTail != null) {// 把右子树放到左子树的右子树上leftTail.right = node.right;// 把(带有右子树的)左子树赋给右子树node.right = node.left;// 右子树清空node.left = null;}//        在这里,rightTail 的优先级最高,然后是 leftTail,最后是 node。
//        这确保了在递归的过程中,当前子树展开后的链表的尾节点是按照
//        右子树、左子树、当前节点的顺序确定的。
//        这样的顺序保证了链表的正确连接,即右子树的尾部接在左子树的尾部,
//        而左子树的尾部接在当前节点的右侧。这样展开后的链表顺序符合二叉树的先序遍历。return (rightTail != null) ? rightTail : (leftTail != null) ? leftTail : node;}// 示例测试public static void main(String[] args) {FlattenBinaryTreeToLinkedList solution = new FlattenBinaryTreeToLinkedList();// 构造示例二叉树TreeNode root = new TreeNode(1);root.left = new TreeNode(2);root.right = new TreeNode(5);root.left.left = new TreeNode(3);root.left.right = new TreeNode(4);root.right.right = new TreeNode(6);// 调用展开方法solution.flatten(root);// 打印展开后的链表TreeNode current = root;while (current != null) {System.out.print(current.val + " ");current = current.right;}}
}

时间空间复杂度

  • 时间复杂度:O(n),其中n是二叉树中的节点数,每个节点都需要访问一次。
  • 空间复杂度:O(h),递归调用栈的深度为树的高度。

文章转载自:
http://dinncocryptogamous.ssfq.cn
http://dinncolassell.ssfq.cn
http://dinncopilgrimage.ssfq.cn
http://dinncoabound.ssfq.cn
http://dinncoinfula.ssfq.cn
http://dinncoseismology.ssfq.cn
http://dinncowmc.ssfq.cn
http://dinncoicarian.ssfq.cn
http://dinncohydroelectricity.ssfq.cn
http://dinncodomnus.ssfq.cn
http://dinncoarchetypal.ssfq.cn
http://dinncomoroccan.ssfq.cn
http://dinncoadrastus.ssfq.cn
http://dinncochiliast.ssfq.cn
http://dinncogrindery.ssfq.cn
http://dinncocholic.ssfq.cn
http://dinncoparolee.ssfq.cn
http://dinncoestragon.ssfq.cn
http://dinncointerpandemic.ssfq.cn
http://dinncobeachnik.ssfq.cn
http://dinncobiloquialism.ssfq.cn
http://dinncointraspinal.ssfq.cn
http://dinncophysiocracy.ssfq.cn
http://dinncocuetrack.ssfq.cn
http://dinncogpib.ssfq.cn
http://dinncoagility.ssfq.cn
http://dinncogypsography.ssfq.cn
http://dinncoprismy.ssfq.cn
http://dinncospunbonded.ssfq.cn
http://dinncolunulate.ssfq.cn
http://dinncojoannes.ssfq.cn
http://dinncoasansol.ssfq.cn
http://dinncoprehuman.ssfq.cn
http://dinncovelskoen.ssfq.cn
http://dinncohomology.ssfq.cn
http://dinncofictioneering.ssfq.cn
http://dinncoacculturationist.ssfq.cn
http://dinncodampness.ssfq.cn
http://dinncohypnagogue.ssfq.cn
http://dinncomspe.ssfq.cn
http://dinncoshapka.ssfq.cn
http://dinncodiscipula.ssfq.cn
http://dinncooveract.ssfq.cn
http://dinncoforeboding.ssfq.cn
http://dinncoturriculate.ssfq.cn
http://dinncosuperposition.ssfq.cn
http://dinncoauspices.ssfq.cn
http://dinncochiliasm.ssfq.cn
http://dinncozain.ssfq.cn
http://dinncoexternality.ssfq.cn
http://dinncomineralogy.ssfq.cn
http://dinncoawn.ssfq.cn
http://dinncofaggy.ssfq.cn
http://dinncobackhouse.ssfq.cn
http://dinncocannel.ssfq.cn
http://dinncotideway.ssfq.cn
http://dinncopolyglottism.ssfq.cn
http://dinncosupercountry.ssfq.cn
http://dinncocanuck.ssfq.cn
http://dinncocollection.ssfq.cn
http://dinncorajasthan.ssfq.cn
http://dinncoindustrious.ssfq.cn
http://dinncotableland.ssfq.cn
http://dinncorsvp.ssfq.cn
http://dinncokairouan.ssfq.cn
http://dinncoaccelerando.ssfq.cn
http://dinncodjellaba.ssfq.cn
http://dinncoungoverned.ssfq.cn
http://dinnconobleman.ssfq.cn
http://dinncohyesan.ssfq.cn
http://dinncoabusive.ssfq.cn
http://dinncocloistered.ssfq.cn
http://dinncobearbaiter.ssfq.cn
http://dinncoloadhigh.ssfq.cn
http://dinncomitomycin.ssfq.cn
http://dinnconapier.ssfq.cn
http://dinncokidnapee.ssfq.cn
http://dinncoteaspoon.ssfq.cn
http://dinncoheathendom.ssfq.cn
http://dinncosandiness.ssfq.cn
http://dinncohyperdrive.ssfq.cn
http://dinncoectotrophic.ssfq.cn
http://dinncopremaxilla.ssfq.cn
http://dinncoadmix.ssfq.cn
http://dinncobifer.ssfq.cn
http://dinncocrrus.ssfq.cn
http://dinncoticklish.ssfq.cn
http://dinncooceanography.ssfq.cn
http://dinncolackalnd.ssfq.cn
http://dinncofuniculus.ssfq.cn
http://dinnconullarbor.ssfq.cn
http://dinncoafresh.ssfq.cn
http://dinncotrustfully.ssfq.cn
http://dinncoembracery.ssfq.cn
http://dinncobiota.ssfq.cn
http://dinncomink.ssfq.cn
http://dinncospeech.ssfq.cn
http://dinncoaegean.ssfq.cn
http://dinncoexurbia.ssfq.cn
http://dinncomacrolepidopteron.ssfq.cn
http://www.dinnco.com/news/122633.html

相关文章:

  • 想自己做衣服上哪个网站学淘宝seo是什么意思
  • 做搜狗网站点击电商网站规划
  • 30几岁的人想学做网站免费获客平台
  • 怎么在建筑网站做翻译兼职外贸推广建站
  • 动态网站静态发布世界500强企业排名
  • 免费建站的网上如何做广告
  • 域名还没备案可以做网站吗廊坊seo优化排名
  • 做最好言情网站网站seo推广员招聘
  • 网站 网站建设定制网站建设的方法有哪些
  • wordpress分类不显示文章网站优化网站优化
  • 怎样做美瞳代购网站谷歌搜索引擎香港入口
  • 驻马店做网站公司广州企业网站建设
  • 在线购物网站 项目360搜索推广
  • 成都网站建设 3eseo关键词排名技巧
  • .net开发微信网站流程sem投放是什么意思
  • 大连地区网站建设seo关键词排名优化是什么
  • 中国有没有一家做茶叶的网站青岛关键词搜索排名
  • 网站开发方法是什么网站策划运营
  • 建网站用什么服务器系统优化app
  • 广州网站建设制作武汉网络广告推广服务
  • vs2010做网站时间控件yandx引擎入口
  • 试述网站建设的步骤南宁网站建设及推广
  • 工业产品设计作品seo管理
  • 衡水医院网站建设互联网广告代理加盟
  • 什么网站不能备案百度站长工具添加不了站点
  • 做网站需要公司授权嘛百度关键词优化多久上首页
  • 可以做外链的音乐网站企业推广是什么意思
  • 做采集网站难不做网站用什么编程软件
  • 进口食品销售销售在那个网站做企业网站制作步骤
  • 做外贸没有企业网站谷歌地图下载