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

【Hot100】LeetCode—114. 二叉树展开为链表

目录

  • 1- 思路
    • 技巧——借助指针
  • 2- 实现
    • ⭐114. 二叉树展开为链表——题解思路
  • 3- ACM 实现


  • 原题连接:114. 二叉树展开为链表

1- 思路

技巧——借助指针

思路:通过 ① 将左子树的右下结点的 .next ——> 拼接到当前节点的右子树上

  • 构造 cur 指针, cur = root
  • 借助 pre 指针和 next 指针
    • pre 指针:用来定位左子树的最右下结点,用来拼接现有结点的右侧子树
    • next 指针:又来记录 cur.next 用来处理,用于 更新现有结点的右子树

  • 1- 第一个 while 条件 **while(cur != null)**
    • 1.1 如果左侧不为 null
      • 定义 next ,记录翻转后的 next 结点,next = cur.left
      • 定义 pre,利用 while,定位 pre 到最右下结点pre = next
  • 2- 更新逻辑
    • 2.1 利用 pre 定位到 左子树的 最右下结点
    • 2.2 prenextcur.next
    • 2.3 cur.right = next
    • 2.4 cur.left = null;
  • 3- 最终移动 cur
    • cur = cur.next

2- 实现

⭐114. 二叉树展开为链表——题解思路

在这里插入图片描述

class Solution {public void flatten(TreeNode root) {TreeNode cur = root;while(cur!=null){if(cur.left!=null){TreeNode next = cur.left;TreeNode pre = next;while(pre.right != null){pre = pre.right;}// 更新逻辑pre.right = cur.right;cur.right = next;cur.left = null;}cur = cur.right;}}
}

3- ACM 实现

public class falttern {public static class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}public static TreeNode build(String str){if(str == null || str.length()==0){return null;}String input = str.replace("[","");input = input.replace("]","");String[] parts = input.split(",");Integer[] nums = new Integer[parts.length];for(int i = 0 ; i < parts.length;i++){if(!parts[i].equals("null")){nums[i] = Integer.parseInt(parts[i]);}else{nums[i] = null;}}Queue<TreeNode> queue = new LinkedList<>();TreeNode root = new TreeNode(nums[0]);queue.offer(root);int index = 1;while(!queue.isEmpty()&& index<parts.length){TreeNode node = queue.poll();if(index<nums.length && nums[index]!=null){node.left = new TreeNode(nums[index]);queue.offer(node.left);}index++;if(index<nums.length && nums[index]!=null){node.right = new TreeNode(nums[index]);queue.offer(node.right);}index++;}return root;}public static void flattern(TreeNode root){TreeNode cur = root;while(cur!=null){if(cur.left!=null){TreeNode pre = cur.left;TreeNode next = pre;while(pre.right!=null){pre = pre.right;}// 处理逻辑pre.right = cur.right;cur.right = next;cur.left = null;}cur = cur.right;}}static List<List<Integer>> res = new ArrayList<>();public static List<List<Integer>> levelOrder(TreeNode root) {if(root==null){return res;}
// 队列Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while(!queue.isEmpty()){int len = queue.size();List<Integer> path = new ArrayList<>();while(len>0){TreeNode nowNode = queue.poll();path.add(nowNode.val);if(nowNode.left!=null) queue.offer(nowNode.left);if(nowNode.right!=null) queue.offer(nowNode.right);len--;}res.add(new ArrayList<>(path));}return res;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);String input = sc.nextLine();TreeNode root = build(input);flattern(root);levelOrder(root);System.out.println("结果是"+res.toString());}
}

http://www.mrgr.cn/news/5681.html

相关文章:

  • 图像处理 -- 仿射变换之Affine Transformation
  • softmax多分类及多任务示例
  • 防范小程序隐私合规风险,筑牢用户信任防线
  • 海康VisionMaster使用学习笔记6-图像拼接
  • Ant-Design-Vue快速上手指南 + 排坑
  • 【C语言】关键字——const
  • 基于TCP服务的TLV编解码
  • OCR文字识别接口如何用Java进行调用
  • Android UI:PopupWindow:API
  • Studying-CodeTop | 3. 无重复字符的最长子串、206. 反转链表、146. LRU 缓存
  • uniapp 地图map画出地市轮廓
  • 2. springboot集成kafka入门使用教程
  • 已解决:java.lang.ClassNotFoundException: com.mongodb.test.test 异常的正确解决方法,亲测有效!!!
  • 进阶岛 - MindSearch(CPU版)部署到github codespace
  • 软件测试-测试分类
  • 独立ip为何高级又安全?
  • Scratch与AI:开启少儿编程的智能之旅
  • 编译原理(极速版)
  • 【简历】25届青岛某一本JAVA简历:中厂不要强调算法,面试官听不懂
  • Nginx(项目管理和LINUX)