LeetCode刷题笔录Flatten Binary Tree to Linked List

告别复杂配置:dbswitch vs 传统工具(Kettle/DataX)在Greenplum数据同步中的性能对比 本文深入对比了新兴工具dbswitch与传统ETL工具Kettle、DataX在Greenplum数据同步中的表现。重点分析了dbswitch如何通过免中间文件、利用二进制COPY协议等设计,在异构数据库同步场景下实现更优的性能和更简化的配置,为技术选型提供清晰参考。 阅读详情

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1
        / \
       2   5
      / \   \
     3   4   6

The flattened tree should look like:
   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
             6

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

按照Hint所说的,用一个preorder traversal就搞定了。previous记录前一个TreeNode。这里我用的是非递归的方法。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void flatten(TreeNode root) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if(root == null)
            return ;
        stack.push(root);
        TreeNode previous = null;
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            if(previous == null)
                previous = node;
            else{
                previous.right = node;
                previous.left = null;
                previous = node;
            }
            
            if(node.right != null)
                stack.push(node.right);
            if(node.left != null)
                stack.push(node.left);
        }
        
        return;
    }
}
不用递归还是因为对于递归用的不熟练。

递归的方法在这里有。

当然这个解法是用了extra space的,所以并不是题目要求的解法。上面那个连接里还有一个非递归不用stack的方法,直接参考那里的解法好了。

Part03 数据结构 本文介绍了CSP-S初赛中常见的高级数据结构及其实现,包括: 线性结构:双端栈、双端队列、单调队列、优先队列和ST表,重点分析了它们的特点、时间复杂度及典型应用场景。 集合与森林:并查集(带路径压缩和按秩合并)和树的孩子兄弟表示法,展示了多叉树转化为二叉树存储的方法。 特殊树结构:包括二叉堆、树状数组、线段树和字典树,详细说明了它们的构建、查询和更新操作,特别强调了线段树的递归实现和字典树的前缀匹配特性。 每种数据结构都配有核心C++代码实现,涉及数组模拟、动态规划和树形结构等编程技巧,适合算法竞赛选手快速 阅读详情

相关推荐

LTspice基础教程-024.参数扫描;step指令用法

step指令用于参数扫描,扫描步骤可以是线性的,对数的或列表。 下面介绍几种常用用法: 绘制一简单电阻分压电路,如下: 线性扫描 语法:.step param <Name> <Start Value> <Stop Value> <Increment> 例:.step param Res 20 100 20 意思是R2的阻值从20变化到100,...

gaoyong_wang的博客 3万+

LeetCode刷题笔录 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo

likecool21的专栏 1万+

(OSTrack)Joint Feature Learning and Relation Modeling for Tracking A One-Stream Framework

Joint Feature Learning and Relation Modeling for Tracking A One-Stream Framework (OSTrack)

小火山的博客 1653

LeetCode刷题笔录 Two Sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the

likecool21的专栏 1万+

TI CC2642R蓝牙5.2模块实战:从零搭建智能门锁原型(附电路图)

本文详细介绍了如何基于TI CC2642R蓝牙5.2模块从零搭建智能门锁原型,涵盖硬件选型、低功耗配置、蓝牙安全交互等关键环节。通过CC2642R的高性能MCU和蓝牙5.2技术,开发者可实现低功耗、高安全性的智能门锁设计,并提供实用的电路图和PCB布局建议。

o5p6q的博客 109

保研专业课复习别瞎卷!抓住408和数学重点,用真题反推高效复习路径

本文针对计算机专业保研学生,提出以真题为导向的408专业课和数学高效复习策略。通过构建真题知识图谱、分析题型与难度矩阵,帮助考生精准把握数据结构、操作系统等核心科目的高频考点,如树与图算法、TCP/IP协议栈等,并制定科学的时间分配方案,实现短期内的快速突破。

weixin_27966471的博客 402

LeetCode刷题笔录 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.Th

likecool21的专栏 6928

40分钟快速入门Dart基础(上)

针对final 和const 理解的有点模糊的可以参考这篇文章: 可以参考这里Flutter 知识梳理(Dart)-Dart中static final const 区别看是不是超级好上手,好操作~其实Dart的整个学习起来还是蛮简单的,对于有基础的老司机更简单一些。分分钟敲起来~这篇文章就简单的介绍到这里如何短时间快速入门Dart,避免入坑。由于是想让大家快速入门Flutter,所以针对上面的这些章节我们讲解的只是一些在开发Flutter 过程中常用的一些方法和函数。

qq_7813180 4572

Stanford Algorithms学习:Clustering 2

这是紧接上面的一道题,比较有趣 Question 2 In this question your task is again to run the clustering algorithm from lecture, but on a MUCH bigger graph. So big, in fact, that the distances (i.e., edge costs

likecool21的专栏 2567

昇腾AI原生创新算子挑战赛(S1赛季)复盘-LessEqual算子

【代码】昇腾AI原生创新算子挑战赛(S1赛季)复盘-LessEqual算子。

william_myq的博客 507

YOLOv11改进有效系列目录 - 包含卷积、主干、检测头、注意力机制、Neck上百种创新机制 - 针对多尺度、小目标、遮挡、恶劣天气等问题

在一些特定情况下,目标检测任务由于多种因素的影响,变得更加复杂和困难。以下是几类常见的困难目标检测场景:1. 小目标检测定义:小目标检测是指当目标在图像中的占比非常小时,模型需要准确识别和定位这些目标。典型场景包括远程监控、卫星图像分析等。挑战1. 低分辨率:由于目标在图像中占据像素较少,细节缺失,特征提取难度增加。2. 特征不足:小目标缺乏明显的形状和纹理特征,容易被复杂背景淹没。3. 检测难度大:由于小目标与背景之间的对比度较低,误检的几率上升。4. 数据不平衡。

qq_64693987的博客 4万+

Stanford Algorithms学习:Clustering

第二周的第一个编程作业,是利用贪婪算法来实现一个clustering的问题,和ML里学的unsupervised learning差不多。 Question 1 In this programming problem and the next you'll code up the clustering algorithm from lecture for computing a

likecool21的专栏 2507

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

LeetCode 刷题笔录 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link

likecool21的专栏 1806

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LeetCode刷题笔录Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word:

likecool21的专栏 1296

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LeetCode刷题笔录Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

likecool21的专栏 1109

LeetCode刷题笔录 Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题比较简单

likecool21的专栏 1043

LeetCode刷题笔录Unique Binary Search Trees

这里有个讲解(印度人口音真难懂)

likecool21的专栏 1043

LeetCode刷题笔录Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin

likecool21的专栏 1001

LeetCode刷题笔录 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 随手写的一个,正常的读出每一位的数字然后反序: import java.lang.Math; public class Solution { public int revers

likecool21的专栏 941

LeetCode刷题笔录Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

likecool21的专栏 878

LeetCode刷题笔录Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

likecool21的专栏 865

LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai

likecool21的专栏 855

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 365

LeetCode 每日一题 2026/11/2-2026/11/8

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步

AlphaTao的博客 175

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 207

Hash表:两数之和、字母异位词分组、最长连续序列 [力扣 Hot100]

三道高频哈希表应用题:两数之和(利用HashMap快速查找补数)、字母异位词分组(排序后作为键分组)、最长连续序列(通过HashSet去重并仅从序列起点扩展)。核心思想均为借助哈希结构优化查找效率,避免重复计算。同时提及双指针等其他解法思路,体现算法设计中“空间换时间”的典型策略。

2403_87560480的博客 218
上一篇: LeetCode刷题笔录Unique Paths II
下一篇: LeetCode刷题笔录Subsets
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值