leetcode question 106 :Construct Binary Tree from Inorder and Postorder Traversal

深度学习中的指标总结 对于一个二分类问题,我们定义如下指标: TP:True Positive,即正确预测出的正样本个数 FP:False Positive,即错误预测出的正样本个数(本来是负样本,被我们预测成了正样本) TN:True Negative,即正确预测出的负样本个数 FN:False Negative,即错误预测出的负样本个数(本来是正样本,被我们预测成了负样本) 1.分类: 1)正确率(accuracy)   正确率是我们最常见的评价指标,accuracy = (TP+TN)/(P... 阅读详情

问题:

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
   / \
  9  20
    /  \
   15   7


分析:

这道题和由前序和中序求出这棵树的道理是一样的,只不过根节点在后序排列数组的最后一个而已,详细可以看leetcode 105.


/**
 * 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* buildTree(vector<int>& inorder, vector<int>& postorder) {
        TreeNode* res;
        if( postorder.size() == 0 ){//树是空的
            return NULL;
        }
        
        res = new TreeNode(postorder[postorder.size()-1]);
        if( postorder.size() == 1 ){//只剩下一个节点
            return res;
        }
        
        int index;
        for( int i = 0 ; i < inorder.size() ; i++ ){
            if( inorder[i] == postorder[postorder.size()-1] ){//找到根节点在中序排序的位置
                index = i;
                break;
            }
        }
        
        vector<int> postLeft;
        vector<int> postRight;
        vector<int> inLeft;
        vector<int> inRight;
        for( int i = 0 ; i < index ; i++ ){//以找到的下标为界,构建出两个子树的中序和后序遍历
            inLeft.push_back( inorder[i] );
            postLeft.push_back( postorder[i] );
        }
        
        for( int i = index + 1 ; i < inorder.size() ; i++ ){
            inRight.push_back(inorder[i]);
            postRight.push_back( postorder[i-1] );
        }
        
        res->left = buildTree( inLeft , postLeft );//递归求解
        res->right = buildTree( inRight , postRight );
        
        return res;
    }
};

【XR技术介绍】深度解析|AI眼镜核心技术架构:从硬件层到端云AI全链路拆解 本文将逐层拆解AI眼镜完整技术架构,梳理硬件、感知、光学、端侧AI、软件云端五大核心层级,剖析核心数据流与行业技术瓶颈,同时附上标准化技术架构图,适合技术学习、方案汇报、行业科普使用。 阅读详情

相关推荐

Qcom camera hal简介

Qcom camera hal简介

weixin_42136255的博客 7792

进化算法总览

1.遗传算法 2.差分进化算法 https://zhuanlan.zhihu.com/p/112471225

csdn609387481的博客 667

嵌入式面试|MCU+RTOS技术栈——面试八股文整理3:STM32

stm32

weixin_44800059的博客 1925

leetcode question 105 : 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.For example, givenpreorder = [3,9,20,15,7] inorder = [9,3,15,2...

csdn609387481的博客 240

STM32F407+LAN8720A+LwIP实战:FreeModbus TCP从机功能开发指南

本文详细介绍了基于STM32F407和LAN8720A硬件平台,结合LwIP协议栈实现FreeModbus TCP从机功能的开发指南。内容涵盖硬件连接、LwIP移植、FreeModbus TCP从机实现、任务调度及调试技巧,为工业控制领域的以太网通信开发提供实用参考。

weixin_29190651的博客 554

window下安装并使用nvm(含卸载node、卸载nvm、全局安装npm)

文章目录一、卸载node二、安装nvm三、配置路径和下载源四、使用nvm安装node五、nvm常用命令六、卸载nvm七、遇到的问题 nvm 全名 node.js version management,顾名思义是一个nodejs的版本管理工具。通过它可以安装和切换不同版本的nodejs。 一、卸载node 如果你已经安装了node,那么你需要先卸载node,如果你没有安装那直接跳过这一步到下一步。 打开控制面板 -> 打开程序和功能 -> 右上角搜索输入node -> 右键卸载 为了确保彻底

HuangsTing的博客 41万+

leetcode question 199:Binary Tree Right Side View

问题: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Example: Input: [1,2,3,null,5,null,4] Output: [1,...

csdn609387481的博客 232

内网CentOS离线部署LibreOffice:从依赖缺失到服务启动的完整避坑指南

本文详细介绍了在CentOS内网环境中离线部署LibreOffice的完整流程,包括依赖包准备、安装顺序优化及常见报错解决方案。针对金融等封闭网络场景,提供从rpm包选择到服务启动的全套避坑指南,特别解决libXinerama等图形库缺失问题,确保文档转换服务稳定运行。

weixin_33416697的博客 252

leetcode question 41:First Missing Positive

问题:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.分析...

csdn609387481的博客 229

嵌入式STM32控制电梯面板

嵌入式STM32控制电梯面板 用点阵来显示电梯向上还是向下运动,用数码管来显示楼层数(由于灯阵显示楼层数比较困难故用数码管代替)。当按下对应的按钮,就可以去到对应层数,可上升可下降。 #include "stm32f10x.h" #include "stm32f10x_exti.h" #include "stm32f10x_it.h" #define uint unsigned int #define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_10) #define

code_init_的博客 2117

PDD psnl_verification anti_content 生成顺序与环境校验说明

K不是明文 URL,而是从当前href再派生出来的一小段摘要值。页面 URL 变,K会跟着变但K不是直接肉眼可读的 URL 字串可以把它理解成“URL 的短摘要字段”。这一步是最容易被误解的地方。它吃进去的是压缩后的中间串不是直接吃环境字段它内部还带有长度预算 / 字符映射 / 位置处理逻辑所以最终看到的环境字段 bytes协议头deflate 压缩自定义编码多层叠加后的结果。先把字段总包做再把压缩结果转成raw再拼fTail最后走做字符级输出因此,当前看到的协议字段流。

码王吴彦祖的博客 76

leetcode question 26:Remove Duplicates from Sorted Array

问题: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 by modifying t...

csdn609387481的博客 229

王者荣耀AI绝悟如何选英雄?

王者荣耀AI绝悟如何选英雄? 【抢射手?抢中单位?「绝悟」在打王者荣耀时是如何选英雄的?】 腾讯 AI Lab 开发的 AI 智能体「绝悟」已让王者峡谷不再只是人类召唤师的竞技场,而且这个 AI 战队在上月底进化成了「完全体」。在一局完整的比赛中,英雄选择阶段是至关重要的(比如五射手或五法师阵容会有官方劝退)。 近日,腾讯 AI Lab 和上海交通大学发布的一篇论文介绍了绝悟的英雄选择策略:Ju...

不断学习,不断进步,提高自己 1万+

leetcode question 113: Path Sum II

问题: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 2...

csdn609387481的博客 227

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

leetcode question 18: 4Sum

问题:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution se...

csdn609387481的博客 223

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

leetcode question 100:Same Tree

题目:Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example 1...

csdn609387481的博客 220

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

leetcode question 42: Trapping Rain Water

问题:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], r...

csdn609387481的博客 211

leetcode question 144:Binary Tree Preorder Traversal

问题: Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, ...

csdn609387481的博客 209

leetcode question 102 : Binary Tree Level Order Traversal

问题:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 /...

csdn609387481的博客 206

leetcode question 31:Next Permutation

问题:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible ...

csdn609387481的博客 206

leetcode question 129: 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-&gt;2-&gt;3 which represents the number 123. Find the...

csdn609387481的博客 202

leetcode question 94:Binary Tree Inorder Traversal

题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2].Note: Recursive solution is trivial, co...

csdn609387481的博客 201

LeetCode 热题 100-普通数组

因为nums[i]应该在的位置索引是nums[i]-1,所以如果nums[nums[i]-1](按索引找出来的值)不是nums[i],那就换位置。lambda是匿名函数,lambda表达式本身产生一个函数对象,可以赋值给变量,传给 函数,立即调用。找半天错误,发现不该和intervals前一个区间做对比,应该和list1的最后一个区间做对比。我原本的想法是创建一个新列表,把轮转后的数字放在新列表中,然后把新列表赋值给nums。列表原地排序,排序依据是每个元素(一个区间)的第 0 个值,即左端点。

m0_74153997的博客 166

LeetCode100 22.括号生成

该解法通过深度优先搜索生成所有合法括号组合。使用递归时传入剩余左括号数 left 和右括号数 right,剪枝条件为 left > right(防止右括号多于左括号),非法则返回。每次递归尝试添加左括号或右括号,当两者均为0时,得到一组合法结果。因字符串不可变,无需回溯,直接传递新串。最终返回所有合法括号组合。

Shaoxingzong的博客 160

LeetCode Hot 100 二分查找专题 34 / 33 / 153(Java)

专题主线:三道题共用同一副二分骨架,差别只在循环条件和收缩方向: 34.在排序数组中查找元素的第一个和最后一个位置:用 left <= right 练「边界二分」, 153.寻找旋转排序数组中的最小值:用 left < right + right = mid 练「下界模板」, 33.搜索旋转排序数组:是二者的组合应用。

2401_88612756的博客 570

LeetCode //C - 1325. Delete Leaves With a Given Value

This problem involves removing all leaf nodes from a binary tree that have a given target value, including cases where parent nodes become new leaves with the target value after deletion. The solution uses a postorder traversal approach (left-right-root) t

Made in Code 405
上一篇: leetcode question 105 : Construct Binary Tree from Preorder and Inorder Traversal
下一篇: 在pycharm使用caffe:永久绑定Python路径
橙子爱吃蛋
博客等级 码龄11年 36粉丝 54原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值