Largest BST in a Binary Tree

Zemax实现微透镜阵列光束整形(原理+仿真) 通过Zemax实现微透镜阵列的光场整形仿真。 阅读详情

一到面试题,挺有意思的。

简单做法是对于每个node都进行一遍isBST的测试,找出最大的那个。

public static int largestBSTNaive(TreeNode root){
		if(isBST(root)){
			return size(root);
		}
		return Math.max(largestBSTNaive(root.left), largestBSTNaive(root.right));
	}

	private static int size(TreeNode root) {
		if(root == null)
			return 0;
		return 1 + size(root.left) + size(root.right);
	}

	private static boolean isBST(TreeNode root) {
		
		return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
	}
	
	private static boolean isBST(TreeNode root, int min, int max) {
		if(root == null)
			return true;
		if(root.val >= max)
			return false;
		else if(root.val <= min)
			return false;
		
		return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
	}

上面的做法是top-down的访问,其实可以用bottom-up的方法,根据左子树和右子树返回的信息就可以判断当前子树是不是BST。每个子树需要返回的信息有三个:

1. 子树是不是BST

2.MAX VALUE in this sub tree

3.min value in this sub tree

首先递归得到左右子树的结果,然后如果当前node.val>left.max&&node.val<right.min,切左右子树都是BST,则已当前node为根的子树也是BST。

private static int res = 0;
	public static int largestBSTBetter(TreeNode root){
		largestBSTHelper(root);
		return res;
	}
	
	private static Data largestBSTHelper(TreeNode root){
		Data curr = new Data();
		if(root == null){
			curr.isBST = true;
			curr.size = 0;
			return curr;
		}
		
		Data left = largestBSTHelper(root.left);
		Data right = largestBSTHelper(root.right);
		
		curr.min = Math.min(root.val, Math.min(right.min,left.min));
		curr.max = Math.max(root.val, Math.max(right.max, left.max));
		if(left.isBST && root.val > left.max && right.isBST && root.val < right.min){
			curr.isBST = true;
			curr.size = 1 + left.size + right.size;
			if(curr.size > res)
				res = curr.size;
		}
		else{
			curr.size = 0;
		}
		return curr;
	}
}


class Data{
	boolean isBST = false;
	//the minimum for right sub tree or the maximum for right sub tree
	int min = Integer.MAX_VALUE;
	int max = Integer.MIN_VALUE;
	//if the tree is BST, size is the size of the tree; otherwise zero
	int size;
}


KingSCADA实现按钮点击效果 哈喽,你好啊,我是雷工!在做SCADA项目的时候,按钮是不可缺少的功能,但软件自带的按钮太丑,已经无法满足现如今客户对界面美观度的要求。这时候就需要UI小姐姐设计美观大气的SCADA界面,但UI设计的按钮切图放到程序中只是图片素材,当添加了按钮功能时,虽然能实现按钮功能,但没有任何点击效果;当遇到有需要连锁条件才能点击的按钮时,操作员无法区分是有连锁点击了没反应,还是系统界面卡死了导致的没点击成功,这时候有点击效果就能很好的区分这种问题。本篇介绍一种实现按钮点击效果的方法,以下为操作过程。 阅读详情

相关推荐

从多项式到神经网络:DPD算法演进史及其在RRU中的工程实现

本文系统梳理了数字预失真(DPD)算法从经典多项式模型、查表法到神经网络驱动的演进历程,并深入探讨了其在基站射频单元(RRU)中面临的硬件资源约束与工程实现挑战。文章为通信设备的技术决策者提供了算法选型、性能权衡及混合架构设计的实用评估框架,助力在性能、功耗与成本间找到最优平衡。

cherry的博客 1075

树状数组从前往后求和,用来解第k大(或小)的数 poj 2985 The k-th Largest Group

大概是最详细的树状数组求区间第K大的讲解了http://www.cnblogs.com/wuyiqi/archive/2011/12/25/2301071.html

Aeolusw的博客 476

PX4固件下VTOL固定翼的3种起飞方式对比:手抛vs弹射vs跑道

本文深度解析PX4固件下VTOL固定翼的三种起飞方式:手抛、弹射与跑道起飞,对比其技术差异与适用场景。通过详细的参数配置和工程实践指南,帮助开发者优化起飞性能,提升无人机在不同环境下的适应性和可靠性。文章特别针对PX4飞控系统的最新特性进行了技术探讨。

weixin_30296405的博客 87

二叉树问题太复杂?「三步走」方法解决它!

点击关注上方“五分钟学算法”,设为“置顶或星标”,第一时间送达干货。转自面向大象编程,作者nettee本文将以两道题目为例,讨论如何拆解复杂的二叉树问题:LeetCode 1372. ...

程序员吴师兄的博客 1940

PPO实战:如何用强化学习让小车爬上MountainCarContinuous-v0山顶(附完整代码)

本文提供了使用PPO算法解决MountainCarContinuous-v0环境的完整实战教程。针对该环境的连续动作空间和稀疏奖励挑战,文章详细解析了PPO智能体的网络架构、状态归一化、GAE优势估计等核心实现,并附带了可直接运行的PyTorch代码。同时分享了关键的调参技巧和实战避坑指南,帮助读者高效训练智能体完成小车爬坡任务。

linux的博客 968

MPU6050数据融合避坑指南:当欧拉角遇到万向节死锁,我们该如何处理?

本文深入探讨了MPU6050在姿态解算中遇到的欧拉角万向节死锁问题,提供了四元数解决方案及工程实践策略。通过分析死锁现象的本质,介绍四元数的优势与实现方法,并给出必须使用欧拉角时的三种应对方案,帮助开发者有效规避数据跳变和解算失效问题。

weixin_30415113的博客 541

二叉树-Largest BST in a tree

http://leetcode.com/2010/11/largest-binary-search-tree-bst-in.html All desc http://leetcode.com/2010/11/largest-binary-search-tree-bst-in_22.html

雯雯熊孩子 479

第三篇:AMD 和 Intel

它像“乐高积木”一样,可以通过标准化的接口与其他部分组合,构建复杂系统。理解组件后,你会发现从操作系统到微服务,现代软件本质上都是组件的组合!

u013089100的博客 1030

【PAT甲级A1099】Build A Binary Search Tree (30分)(c++)

1099 Build A Binary Search Tree (30分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:200 ms 内存限制:64 MB A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys l

小庄同学的博客 211

若依框架(ruoyi vue版)集成jeecg积木报表整体完整实用教程(超长篇幅耐心阅读)

若依框架(ruoyi vue版)集成jeecg积木报表整体完整实用教程,主要说明jeecg积木报表的集成、jeecg积木报表嵌入若依菜单、token权限控制,以及iframe嵌入表格等内容。

weixin_53067723的博客 4639

3.5.3以太网ETH TC8-PMA物理层测试

英文原版-OA_Automotive_Ethernet_ECU_TestSpecification_Layer_1_100BASE-T1_v3.0.pdf。PMA测试时各个测试项目所进入的测试模式不同,如TX_01对应测试模式1;TX_04对应测试模式5;例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。TX_07对应测试模式5;物理层测试主要分为两部分:IOP测试(包括链接时间,信号质量,线路诊断),PMA测试。

qq_28664971的博客 7690

1099 Build A Binary Search Tree (30 分)

1099 Build A Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node

qq_43492878的博客 221

2篇5章1节:实验流行病学的核心特征和设计原则

本文系统阐释实验流行病学的核心定义、四大核心特征、四大基本原则及主流设计类型,结合骨科、肿瘤科、儿科、精神科、内外妇儿及公共卫生等多学科临床实践案例,具象化解读人为干预、随机分组、平行对照、前瞻性设计的核心内涵与应用价值。同时,规范梳理对照、随机、盲法、重复四大研究基本原则,详细介绍临床试验、现场试验、社区试验三类主流研究设计的适配场景与应用差异。

MD Code 165

(二叉搜索树6-10)The Kth Largest in BST--C语言

用中序遍历得到序列,因为是二叉搜索树,则该序列一定是递增的,所以直接返回序列中倒数第k个数(下标为count-k),count为序列总数。

2302_80061132的博客 1779

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Kth Smallest Element in a BST

题目:   Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if

xiaohuihui52309的专栏 298

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Middle-题目16:230. Kth Smallest Element in a BST

题目原文: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. 题目大意: 求一个BST的第k小的节点。 题目分析: 根据二叉排序树的性质,中序遍历序列第k个元素即为所求。 源码:(language:java)public class Soluti

376

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Examp...

weixin_33850890的博客 124

5-1 Build A Binary Search Tree (30分)

5-1 Build A Binary Search Tree   (30分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with ke

csucbMIT的博客 1778

【LeetCode & 剑指offer刷题】树题16:Kth Smallest Element in a BST

【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, write a functionkthSmallestto find thekth smallest element in it. Note: You may...

技术改变世界 233

leetcode笔记-Kth Smallest Element in a BST

思路:定义一个中序遍历的函数,调用中序遍历将各个节点都存在VECTOR中,其中的数是从小到大的排列的,再取第K个最小的值 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeN

ldon的博客 535

A1099 Build A Binary Search Tree (30 分)

#include&lt;cstdio&gt; #include&lt;algorithm&gt; #include&lt;queue&gt; using namespace std; const int maxn = 110; struct Node { //int data; int left, right; }node[maxn]; int n,level[maxn],origin[max...

A_Aria的博客 188

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 543

[leetcode]230. Kth Smallest Element in a BST

[leetcode]230. Kth Smallest Element in a BST Analysis ummmm—— [每天刷题并不难。。。其实已经很多天没刷了] Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may ...

weixin_32135877的博客 221

[PAT] A1143/A1151 Lowest Common Ancestor

A1143/A1151 Lowest Common Ancestor1143 Lowest Common Ancestor1151 LCA in a Binary Tree 1143 Lowest Common Ancestor Link 1143 Lowest Common Ancestor (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U a

J.DiCaprio的博客 339

[算法分析与设计] leetcode 每周一题: Kth Smallest Element in a BST

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note:  Y

liangtjsky的博客 280

leetCode(46):Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note:  You may assume k is always valid, 1 ≤ k ≤ BST's total elements.         直观地一想。查找第...

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值