LeetCode刷题笔录 Binary Tree Inorder Traversal

数据资产入表——解读100页数据资产入表解读与分析【附全文阅读】 本文档《数据资产入表:解读与分析》面向企业财务、数据管理、法务合规及高管等核心岗位,系统解析数据资产入表全流程。以"政策-实操-价值"为主线,首先解读入表政策要求,明确会计处理规范;其次详细拆解数据盘点、治理、确权、评估等实操步骤,提供行业案例参考;最后分析入表难点及后续运营策略,为企业提供从政策理解到落地执行的一站式解决方案。 阅读详情

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?


这题和Preorder traversal的区别重点在于一个node第一次被从stack中pop出来时,不对其进行访问,而是要将其放回stack。在第二次pop时再进行访问。

在纸上画一画就可以得到以下要点:

1.需要记录一个node是第几次被从stack中pop

2.第一次pop出一个node后,按照right child, node, left child的顺序将这三个node(如果存在)放回stack

在这里我用了一个Stack<Integer> counterStack来记录每个node是第几次被pop出stack。初始值是0,第一次被pop后数值变为1. 两个stack的元素一一对应,这样就可以知道当前node的count。

public class Solution {
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<Integer>();
        if(root == null)
            return result;
        Stack<TreeNode> nodeStack = new Stack<TreeNode>();
        Stack<Integer> counterStack = new Stack<Integer>();
        nodeStack.push(root);
        counterStack.push(0);
        
        while(!nodeStack.isEmpty()){
            TreeNode node = nodeStack.pop();
            int count = counterStack.pop();
            //if this is the second time that this node is popped out, visit it
            if(count == 1){
                result.add(node.val);
            }
            //else push the node's right child to the stack, itself back to the stack, add its counter, and adds its left child
            else{
                if(node.right != null){
                    nodeStack.push(node.right);
                    counterStack.push(0);
                }
                nodeStack.push(node);
                counterStack.push(1);
                if(node.left != null){
                    nodeStack.push(node.left);
                    counterStack.push(0);
                }
            }
        }
        
        return result;
    }
}


图像质量评估和点云质量评估 本文介绍了图像质量评估和点云质量评估的基本概念和常用方法,并提供了相应的源代码示例。图像质量评估关注图像的视觉质量,常用的评估指标包括均方误差(MSE)、结构相似性指标(SSIM)和峰值信噪比(PSNR)。点云质量评估关注点云数据的准确性和完整性,常用的评估方法包括点云配准质量评估、稠密重建质量评估和离群点检测质量评估。在计算机视觉和计算机图形学领域中,图像质量评估和点云质量评估是两个重要的任务。图像质量评估旨在量化图像的视觉质量,而点云质量评估则关注于点云数据的准确性和完整性。 阅读详情

相关推荐

光学设计必知:Ansys Zemax中6种系统孔径类型的适用场景全解析

本文深入解析Ansys Zemax中6种系统孔径类型的适用场景,包括入瞳直径、像方空间F/#、物方空间NA等,帮助光学设计师精准控制光线选择和成像质量。通过实际案例和代码示例,详细讲解各孔径类型的计算原理、参数设置及在显微系统、激光扩束等场景中的优化策略,提升复杂光学系统设计效率。

weixin_29266007的博客 551

Binary Tree Inorder Traversal 二叉树的中序遍历

给定一个二叉树,返回它的中序 遍历。示例:输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2]进阶: 递归算法很简单,你可以通过迭代算法完成吗?思路:递归的方法简单易懂,这里要求用非递归的方法实现,对于中序遍历(左-中-右)我们需要一个栈,首先压入根节点,然后循环压入左节点,如果左节点不为空就压出栈顶节点并打印,然后压入栈顶元素的右节...

qq_26410101的博客 300

IC版图设计之OASIS文件格式详解

OASIS(Open Artwork System Interchange Standard)是继GDSII之后新一代的集成电路版图数据交换标准,由SEMI在2004年正式发布(SEMI标准号:SEMI P39)。高压缩率:相比GDSII可减少3-10倍文件体积扩展性强:支持纳米级工艺的复杂图形描述跨平台兼容:独立于EDA工具和操作系统安全增强:内置校验和与加密支持。

ZZcppc的博客 2440

LeetCode刷题笔录Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.

likecool21的专栏 786

Spring AI Alibaba实战训练营-20 基于注解驱动的MCP快速开发入门指南

本文详细讲解如何使用SpringAiAlibaba快速开发基于注解驱动的MCP应用。文章详细介绍了MCP协议并且重点介绍了Spring AI通过注解驱动的开发方式简化MCP服务开发。具体包括服务端和客户端的注解使用、城市最新时间查询MCP项目结构概览(服务端和客户端模块)以及环境配置要求。

program哲学 295

【AI智能体】Spring AI Tool 从使用到实战详解

Spring AI Tool

congge 5864

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万+

构网型逆变器下垂控制与电流限幅策略研究:理论、仿真与代码实现

随着分布式发电和微电网技术的快速发展,构网型(Grid-Forming, GFM)逆变器因其能够自主建立电压和频率参考、支撑弱电网及孤岛运行而受到广泛关注。本文提出并实现了一种基于下垂控制的构网型逆变器控制策略,将逆变器等效为受控电压源,通过L型滤波器接入配电网。在公共耦合点(PCC)处实时测量三相电压和电流,计算有功和无功功率,进而采用P-w和Q-V下垂控制生成内部频率和电压幅值,合成三相参考电压。针对故障情况下过电流问题,设计了一种自适应电流限幅模块:当电流幅值超过设定上限时,动态降低电压参考系数k_s

huanghm88的专栏 155

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万+

Android Camera MTK框架

Camera简介: 虽然前文说道Android在5.0之后就推出了Camera2 API,但是各个厂家的基本未适配,导致目前市场上大部分机型使用的仍然是Camera1 API。PS:Android 9.0 Google强制要求各个平台厂商(高通/MTK/华为等)支持Camera2,不再兼容Camera1。 ...

weixin_49303682的博客 5507

基于 Mask R-CNN 的实例分割:从零开始构建一个强大的分割模型

在本文中,我们将介绍如何使用 Mask R-CNN(Region-based Convolutional Networks with Masks)实现实例分割。实例分割是计算机视觉中的一种任务,旨在识别并分割图像中的各个对象。我们将讨论如何准备数据集、配置 Mask R-CNN 模型并在自定义数据集上进行训练。

斌擎科技 1243

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

科研级YOLO26下采样革新:ContextGuided模块赋能,让特征降维兼具上下文感知的实战指南

在文件夹下新建,将上述类的代码写入其中。这一步是为了让YOLO26的代码架构能识别我们的自定义下采样模块。

LXY_LOVE36的博客 100

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

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

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刷题笔录 Binary Tree Preorder Traversal
下一篇: LeetCode刷题笔录 Populating Next Right Pointers in Each Node
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值