leetcode-105&106 Construct Binary Tree from Preorder and Inorder Traversal I&II

西汉BC94年世界历史地图矢量文件SHP格式.zip 本资源为《西汉BC94年世界历史地图矢量文件SHP格式》压缩包,文件大小约 2.22 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。 立即下载

一、105-Given preorder and inorder traversal of a tree, construct the binary tree.

思路简单,但是实现起来就复杂了。

思路:发现,前序的第一个为根,然后在中序中找到根,在中序中,根的左边是左子树,右边是右子树。然后递归进入左子树与右子树。

可是由于在左子树的时候,坐标会错位,没办法对齐,纠结了很久,还是无法解决。最后求助于强大的百度……发现了大神解决的思路。

说到底也很简单,就是用两个不同的坐标来分别指示前序和后序。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    void insert(vector<int>& preorder, vector<int>& inorder,TreeNode *& tree,int preN, int inN, int len)
    {
        if(len <= 0)//检验边界条件
        {
            tree = NULL;
            return;
        }
        int i;
        for( i=0; i < len; i++)//寻找中序遍历中的根节点位置
        {
            if(inorder[inN+i] == preorder[preN])
            {
                break;
            }
        }
        tree = new TreeNode(preorder[preN]);
        insert(preorder, inorder, tree->left, preN+1, inN, i);
        insert(preorder, inorder, tree->right, preN+i+1, inN+i+1, len-1-i);

    }

    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        int l=preorder.size(); 
        TreeNode *root;
        insert(preorder, inorder,root, 0, 0, l);
        return root;
    }

二、106-Given inorder and postorder traversal of a tree, construct the binary tree.
与上一题类似。
只是将前序换成了后序。后序的最后一个是根。然后找到跟在中序的位置,位置左边是左子树,右边是右子树。然后递归。
采用上题中的解决办法,采用两个坐标,分别指示在中序和后序中的位置。
代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    TreeNode* build(vector<int>& inorder, vector<int>& postorder, TreeNode* &tree, int inN, int posN, int len)
    {
        if( len <= 0) 
        {
            tree = NULL;
            return NULL;
        };
        int i;
        for( i=0; i < len; i++)
        {
            if( inorder[inN+i] == postorder[posN])//寻找根节点
            break;
        }
        tree = new TreeNode(postorder[posN]);//插入根节点
        build(inorder, postorder, tree->left, inN, posN-(len-i), i);//递归左子树
        build(inorder, postorder, tree->right, inN+i+1, posN-1, len-i-1);//递归右子树
    }

    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int len = inorder.size();
        TreeNode* tree=NULL;
        build(inorder, postorder, tree, 0, len-1, len);
        return tree;
    }
};
开发区管委会想建立公共技术服务平台体系,在成果治理机制上该如何规划?.docx 开发区管委会想建立公共技术服务平台体系,在成果治理机制上该如何规划? 立即下载

相关推荐

省级技术交易市场目前的年均交易额规模和增长趋势如何?.docx

省级技术交易市场目前的年均交易额规模和增长趋势如何?

leetcode--217&219&220 Contains Duplicate(重复)I&II&III

一、217 Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return

evolone的专栏 723

《WPF 列表视图(ListView)控件详解与使用-WPF 中的控件(22)-列表视图(ListView)》一文的工程文件

《WPF 列表视图(ListView)控件详解与使用&mdash;WPF 中的控件(22)--列表视图(ListView)》一文的工程文件

leetcode--83&82 Remove Duplicates from sorted list I&II

Given a sorted linked list, delete all duplicates such that each element appear only once.For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.去除重复的单链表节点。 时间12ms。 代码:/** *

evolone的专栏 565

SAP OBYC自动过账全解析:从BSX到WRX的会计科目配置指南

本文深入解析SAP OBYC自动过账功能,从BSX到WRX的会计科目配置全流程,涵盖评估分组代码、事务码和评估类等核心概念。通过实战案例展示如何实现库存变动到财务过账的自动化流转,提升企业财务数据的准确性和效率。

weixin_29239279的博客 431

Windows下构建ImGui.Net

工具 Visual Studio 2017 操作 下载项目 git clone https://github.com/mellinoe/ImGui.NET.git 编译 右键以下两个项目并调试: ImGui.NET.SampleProgram ImGui.NET.SampleProgram.XNA Error fix 中间遇到一些问题: SDK版本不对 去微软官网下载安装对应版本 由于设置了代理被远程服务器拒绝连接 关闭代理(也许在环境变量中) nuget源无对应资源,404 NOT FOUN

jojozym的博客 1495

leetcode-206 Reverse Linked List

Reverse a singly linked list. 题意简单,考察对链表的熟练程度。主要注意链表指针的变化。代码如下:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), ne

evolone的专栏 519

stata 代码实现熵值法计算 含常见问题解答

2.标准化①正向指标②负向指标。

m0_74825360的博客 1221

leetcode-204 Count Primes

Count the number of prime numbers less than a non-negative number, n. 计算小于n的素数的个数。 素数,从2开始,2,3,5,7,11…… 素数定义,除了被1和自身整除外,没有其他因数。 最直接的想法就是从2开始到n的平方根,如果有能整除的,就不是素数。 最直接的往往效率最低。果然,虽然不断剪枝,但是本身的效率低下,再怎么

evolone的专栏 508

非华为电脑也能用!华为电脑管家完美安装教程来了

GitHub 上有一个开源项目 ​****​,专门解决“非华为设备无法安装华为电脑管家”的问题。它通过智能模拟 + DLL 劫持技术,绕过官方对硬件型号的检测,让你在联想、戴尔、华硕甚至 DIY 主机上也能流畅使用华为电脑管家!⭐ 项目已获 ​700+ Stars​,社区活跃,持续更新,兼容性优秀!科技不该有围墙。如果你成功在非华为电脑上跑起了 PCManager,欢迎在评论区晒出你的设备型号 + 使用体验!也别忘了把这篇推文分享给同样“眼馋”华为生态的朋友 ~📱 ​关注我们。

程序员朱永胜 5100

自旋玻璃(spin glass)、自旋冰(spin ice)和量子自旋液体(quantum spin liquid)(之二)

具有相同电荷的电子相互排斥。结果,为了在极其拥挤的环境中相互超越,他们不得不改变自己的行为。剑桥大学和英国伯明翰大学于 2009 年 7 月发表的研究表明,电子可以通过量子隧穿从金属表面跳到相近位置的量子线上,这样做后,将分离成两个准粒子,命名为的自旋子和全子。Van den Brink、Khomskii 和 Sawatzky 在 1997-1998 年对轨道子进行了理论上的预测。在 2011 年 9 月发给出版商的论文中报告了它作为一个单独的准粒子的实验观察结果。

qq_32515081的博客 1万+

leetcode-223 Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is n

evolone的专栏 497

arm版再生龙Clonezilla

备份操作系统

sqlora的专栏 2190

leetcode-26&80 Remove Duplicates from Sorted Array I&II

题干: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with

evolone的专栏 494

Spring Boot Jar包加密实战:用ClassFinal与Jasypt打造代码安全堡垒

Spring Boot代码加密与配置保护方案 针对Spring Boot项目源码和配置易被反编译的风险,本文提出ClassFinal字节码加密+Jasypt配置加密的组合方案。ClassFinal通过Maven插件对核心代码加密,运行时需JavaAgent加载,反编译后仅保留空方法体;Jasypt则采用强加密算法保护敏感配置。方案优势包括: 双重保护:ClassFinal加密业务逻辑,Jasypt加密数据库密码等配置 灵活部署:支持密码启动、机器绑定等安全策略 平滑集成:兼容Spring生态,动态解密结合配

墨夶的博客 1100

【2026OD新机考】【不定滑窗】20260415-API请求日志去重分析【Py/Java/C++/C/JS/Go六种语言OD真题】【欧弟算法】全网注释最详细分类最全的华子OD真题题解

中找到所有的连续的相同元素,同时统计对应的连续相同元素对应的值的平均值。:当右指针为第一个元素(没有前一个元素),或者右指针对应的元素。某微服务系统的日志监控平台需分析API调用记录,日志含大量。相等,说明当前元素仍然属于同一个相同元素窗口,将当前元素值。中的所有元素均相等,并且计算窗口和情况即可。请实现一个函数,对给定的日志数据去重合并处理。位置,表示开启一个新的窗口,此时需要重置窗口和。,为了优化重复的请求记录,存储和后续分析需对。:对应的响应时间数组(毫秒)。重新置为当前右指针的元素值。

weixin_48157259的博客 123

leetcode--160 intersection of two linked list

Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘

evolone的专栏 479

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

leetcode-190&191 Reverse Bits & Number of 1 Bits

一、190 Reverse Bits Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in bina

evolone的专栏 474

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

leetcode-205 Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara

evolone的专栏 458

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

leetcode-10 Regular Expression Matching

Implement regular expression matching with support for ‘.’ and ‘*’.‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element.The matching should cover the entire input string

evolone的专栏 452

leetcode-203 Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5题意清楚,主要考察链表指针的使用。 小心头结点和尾节点的处理。 时间36ms。

evolone的专栏 432

leetcode-202 Happy Number

Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of i

evolone的专栏 413

leetcode --237 delete node in a linked list

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, t

evolone的专栏 390

leetcode-21 Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.题目很简单,就是考察队链表插入的熟悉程度。 12ms。 代码:/** * Definition for singl

evolone的专栏 380
上一篇: leetcode-10 Regular Expression Matching
下一篇: leetcode-21 Merge Two Sorted Lists
evolone
博客等级 码龄14年 399粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值