【重点】【二叉树】114. 二叉树展开为链表

题目

法1:先序遍历

思路不够好

// 递归版遍历
class Solution {
   
    public void flatten(TreeNode root) {
   
        List<TreeNode> nodeList = new ArrayList<>();
        preorder(root, nodeList);
        for (int i = 0; i < nodeList.size(); ++i) {
   
            TreeNode curNode = nodeList.get(i);
            curNode.left = null;
            if (i == nodeList.size() - 1) {
   
                curNode.right = null;
            } else {
   
                curNode.right = nodeList.get(i + 1);
            }
        }
    }
    
    public void preorder(TreeNode root, List<TreeNode> res) {
   
        if (root == null) {
   
            return;
        }
        res.add(root);
        preorder(root.left, res);
        preorder(root.right, res);
    }
}

// 迭代版遍历
class Solution {
   
    public void flatten(TreeNode root) {
   
        List<TreeNode> nodeList = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
   
            if (root != null) {
   
                nodeList.add(root);
                stack.push(root);
                root = root.left;
            } else {
   
                TreeNode tmp = stack.pop();
                root = tmp.right;
            }
        }
        for (int i = 0; i < nodeList.size(); ++i) {
   
            TreeNode curNode = nodeList.get(i);
            curNode.left = null;
            if (i == nodeList.size() - 1) {
   
                curNode.right = null;
            } else {
   
                curNode.right = nodeList.get(i + 1);
            }
        }
    }
}

法2:规律+迭代解法

参考答案很多:https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/solutions/17274/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by–26/?envType=study-plan-v2&envId=top-100-liked
重点记住下面这个!!!

class Solution {
   
    public void flatten(TreeNode root) {
   
        TreeNode cur = root;
        while (cur != null) {
   
            if (cur.left == null) {
   
                cur = cur.right;
            } else {
   
                TreeNode pre = cur.left; // 记录左子树的最右边节点
                while (pre.right != null) {
   
                    pre = pre.right;
                }
                pre.right = cur.right;
                cur.right = cur.left;
                cur.left = null;
                cur = cur.right;
            }
        }
    }
}

相关推荐

  1. 重点】【114. 展开

    2023-12-11 02:26:01       40 阅读
  2. 【算法题】114. 展开

    2023-12-11 02:26:01       26 阅读
  3. 【leetcode热题】展开

    2023-12-11 02:26:01       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 02:26:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 02:26:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 02:26:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 02:26:01       18 阅读

热门阅读

  1. SpringBoot - 四种常见定时器

    2023-12-11 02:26:01       26 阅读
  2. 列表和双向队列的方法

    2023-12-11 02:26:01       32 阅读
  3. qt 模型视图结构

    2023-12-11 02:26:01       35 阅读
  4. TS学习——面向对象

    2023-12-11 02:26:01       37 阅读
  5. 文本转图像 学习笔记

    2023-12-11 02:26:01       39 阅读
  6. 分布式事务实现方案

    2023-12-11 02:26:01       37 阅读
  7. git上传流程

    2023-12-11 02:26:01       38 阅读
  8. MySQL 添加注释(comment)

    2023-12-11 02:26:01       35 阅读
  9. 挖漏洞之文件上传

    2023-12-11 02:26:01       35 阅读
  10. Linux C语言 41-进程间通信IPC之共享内存

    2023-12-11 02:26:01       35 阅读
  11. Linux-实现没有血缘关系的进程之间的通信

    2023-12-11 02:26:01       35 阅读