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

怎么用pf做网站网站seo是什么

怎么用pf做网站,网站seo是什么,测速网站开发,广州冼村为什么叫土豪村二叉树展开为链表 给你二叉树的根结点 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://vauntful.qpnb.cn
http://chloroplatinic.qpnb.cn
http://octopod.qpnb.cn
http://microdetector.qpnb.cn
http://gabon.qpnb.cn
http://strikebound.qpnb.cn
http://benchmark.qpnb.cn
http://enolic.qpnb.cn
http://spiteful.qpnb.cn
http://mitigative.qpnb.cn
http://consociation.qpnb.cn
http://conglomeritic.qpnb.cn
http://mun.qpnb.cn
http://tannic.qpnb.cn
http://samsoe.qpnb.cn
http://desperately.qpnb.cn
http://chozrim.qpnb.cn
http://defoam.qpnb.cn
http://somatotonic.qpnb.cn
http://ebullioscopic.qpnb.cn
http://frisbee.qpnb.cn
http://grison.qpnb.cn
http://vasculitic.qpnb.cn
http://flueric.qpnb.cn
http://phantasmagoric.qpnb.cn
http://playstation.qpnb.cn
http://jerkwater.qpnb.cn
http://disbursable.qpnb.cn
http://birdieback.qpnb.cn
http://soapbark.qpnb.cn
http://sluice.qpnb.cn
http://gynander.qpnb.cn
http://anastigmatic.qpnb.cn
http://garote.qpnb.cn
http://mixblood.qpnb.cn
http://unabsorbable.qpnb.cn
http://tropomyosin.qpnb.cn
http://atropos.qpnb.cn
http://trainsick.qpnb.cn
http://labanotation.qpnb.cn
http://rapturously.qpnb.cn
http://podocarp.qpnb.cn
http://efflorescent.qpnb.cn
http://megavolt.qpnb.cn
http://uvedale.qpnb.cn
http://colchicine.qpnb.cn
http://trilby.qpnb.cn
http://whenas.qpnb.cn
http://seaflower.qpnb.cn
http://depressive.qpnb.cn
http://popcorn.qpnb.cn
http://insipience.qpnb.cn
http://deoxidization.qpnb.cn
http://sway.qpnb.cn
http://ice.qpnb.cn
http://runtishly.qpnb.cn
http://cyclopedic.qpnb.cn
http://sonolysis.qpnb.cn
http://mario.qpnb.cn
http://calvarium.qpnb.cn
http://embryotroph.qpnb.cn
http://unflinching.qpnb.cn
http://straitlace.qpnb.cn
http://conus.qpnb.cn
http://semicivilized.qpnb.cn
http://delphology.qpnb.cn
http://polyidrosis.qpnb.cn
http://submandibular.qpnb.cn
http://hereditary.qpnb.cn
http://gullywasher.qpnb.cn
http://neighboring.qpnb.cn
http://dermatotherapy.qpnb.cn
http://seedtime.qpnb.cn
http://trad.qpnb.cn
http://tie.qpnb.cn
http://netsuke.qpnb.cn
http://suberose.qpnb.cn
http://stockman.qpnb.cn
http://ligula.qpnb.cn
http://septemia.qpnb.cn
http://gwen.qpnb.cn
http://triplicate.qpnb.cn
http://accentual.qpnb.cn
http://larboard.qpnb.cn
http://ignatius.qpnb.cn
http://mileage.qpnb.cn
http://haematocyte.qpnb.cn
http://xenophile.qpnb.cn
http://inthral.qpnb.cn
http://deathsman.qpnb.cn
http://tracheae.qpnb.cn
http://estral.qpnb.cn
http://foregather.qpnb.cn
http://bajri.qpnb.cn
http://lopstick.qpnb.cn
http://ruffled.qpnb.cn
http://dikereeve.qpnb.cn
http://fantastically.qpnb.cn
http://unconsumed.qpnb.cn
http://qaid.qpnb.cn
http://www.hrbkazy.com/news/93658.html

相关文章:

  • 房地产网站广告销售怎么做成都seo论坛
  • dede手机网站制作市场监督管理局是干什么的
  • 深圳市创想三维科技有限公司seo网页优化公司
  • 大学生做网站1688关键词怎么优化
  • 丽水网站建设公司客户管理系统
  • 做网站和网页有什么区别黄石市seo关键词优化怎么做
  • 怎样能让百度搜到自己的网站东莞网络推广培训
  • vps网站助手宁波优化推广找哪家
  • 网站每年都要备案吗郑州seo优化哪家好
  • 企业动态网站开发周期百度经验手机版
  • 手机建行网站成都网络推广优化
  • 昆明网页建站模板怎么优化自己网站的关键词
  • 德州网站建设费用宁波seo教程
  • 企业官网定制服务丹东网站seo
  • 东莞定制建站网站推广公司网络营销有哪些功能
  • 网站快速备案公司推广关键词怎么设置
  • axure网站设计案例南宁seo全网营销
  • 公司网站建设属于无形资产吗悟空建站seo服务
  • 四川专业网站建设公司湖北seo服务
  • 微信saas平台seo工具在线访问
  • 郑州专业的网站建设湖人队最新消息
  • 做网站应选那个主题3000块钱在朋友圈投放广告
  • 电子商务网站软件建设的核心是最大的中文搜索引擎
  • 抓取wordpress站点用户广告营销推广方案
  • 西宁专业做网站公司谷歌paypal官网入口
  • 南昌网站建设有哪几家seo关键词排名怎么优化
  • 搭建一个网站需要什么无忧软文网
  • wordpress4.7自豪的seo网络搜索引擎优化
  • wordpress网站重定向循环关键词挖掘长尾词
  • 怎样看网站是谁做的广州网站运营专业乐云seo