Cracking the coding interview--Q4.5

Google面试之快乐智力题(Cracking the Coding Interview) 问:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药。给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 答:如果药片足够,从第i(1<i<20)个瓶子里取出数量为i的药片称重为Xg,[X-(21*20/2)]/0.1即为装了1.1g药的瓶子的编号。 Q:A bunch of men are on an island. A genie ... 阅读详情

原文:

Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.

译文:

给定二叉查找树的一个结点, 写一个算法查找它的“下一个”结点(即中序遍历后它的后继结点), 其中每个结点都有指向其父亲的链接。


我们知道,在二叉查找树中, 每个结点的值都大于等于它左子树所有结点的值且小于右子树所有结点的值( 或是大于它左子树所有结点的值且小于等于右子树所有结点的值,等号放哪边示情况而定。) 二叉查找树中序遍历后,元素将按递增排序, 某一结点的后继结点即为比该结点大的结点中最小的一个。如果该结点有右儿子, 则后继结点为右儿子的最左子孙。否则需要不断地向上查找该结点的祖先, 直到找到第一个比它大的结点为止。


	public static Node_4_3 nextNode(Node_4_3 node) {
		while(node.lchild != null) {
			node = node.lchild;
		}
		return node;
	}
	
	/**
	 * @param node
	 * @return
	 * 
	 * 获取中序遍历当前结点的next结点
	 * 
	 */
	public static Node_4_3 successor(Node_4_3 node) {
		if(node == null) {
			return null;
		}
		// 如果当前结点存在右子树,则next结点即为右子树最左边结点
		if(node.rchild != null) {
			return nextNode(node.rchild);
		} else {
			// 如果不存在右子树,则next节点往上走
			Node_4_3 parent = node.parent;
			while(parent !=null && parent.rchild == node) {
				parent = parent.parent;
				node = parent;
			}
			return parent;
		}
	}


总代码如下:

package chapter_4_TreesandGraphs;

import java.util.Scanner;

public class Question_4_5 {
	
	public static void create_MinHeight_Tree(Node_4_3 parent, int array[], int start, int end, boolean left) {
		if(start <= end) {
			int mid = (start + end) >> 1;
			Node_4_3 node = new Node_4_3();
			node.data = array[mid];
			node.parent = parent;
			// 构建左子树
			if(left == true) {
				parent.lchild = node;
			} else {
				parent.rchild = node;
			}
			create_MinHeight_Tree(node, array, start, mid-1, true);
			create_MinHeight_Tree(node, array, mid+1, end, false);
		}
	}
	
	public static Node_4_3 nextNode(Node_4_3 node) {
		while(node.lchild != null) {
			node = node.lchild;
		}
		return node;
	}
	
	/**
	 * @param node
	 * @return
	 * 
	 * 获取中序遍历当前结点的next结点
	 * 
	 */
	public static Node_4_3 successor(Node_4_3 node) {
		if(node == null) {
			return null;
		}
		// 如果当前结点存在右子树,则next结点即为右子树最左边结点
		if(node.rchild != null) {
			return nextNode(node.rchild);
		} else {
			// 如果不存在右子树,则next节点往上走
			Node_4_3 parent = node.parent;
			while(parent !=null && parent.rchild == node) {
				parent = parent.parent;
				node = parent;
			}
			return parent;
		}
	}
	
	/**
	 * 
	 * 中序递归遍历
	 * 
	 */
	public static void inOrderTraverse(Node_4_3 node) {
		if(node != null) {
			inOrderTraverse(node.lchild);
			System.out.print("  " +  node.data + "  ");
			inOrderTraverse(node.rchild);
		}
	}
	
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		
		String strs[] = string.split(" ");
		int array[] = new int[strs.length];
		for(int i=0; i<array.length; i++) {
			array[i] = Integer.parseInt(strs[i]);
		}
		
		Node_4_3 head = new Node_4_3();
		int mid = (0 + (array.length-1)) >> 1;
		head.data =  array[mid];
		
		create_MinHeight_Tree(head, array, 0, mid-1, true);
		create_MinHeight_Tree(head, array, mid+1, array.length-1, false);
		
		inOrderTraverse(head);
		
		System.out.println("\nnext :" + successor(head).data);
	}
}


基于HPM5E00的EtherCAT从站开发实战:从硬件解析到软件调试 实时工业以太网通信是工业自动化与运动控制领域的核心技术,它通过硬件与协议栈的协同,实现了微秒级的高确定性数据交换。其技术价值在于为多轴同步、机器人控制等复杂场景提供了高实时性、高同步精度的通信基础。在应用场景上,广泛服务于数控机床、包装机械、半导体设备等高端制造领域。本文聚焦于EtherCAT从站开发,深入解析了基于先楫HPM5E00高性能MCU与专用从站控制器(ESC)的硬件架构设计,并详细阐述了开源协议栈SOES的移植、对象字典配置以及过程数据(PDO)映射等核心软件实践,为开发者快速构建稳定可靠的实时 阅读详情

相关推荐

ESP32-S3数字麦克风入门:手把手教你用Arduino IDE读取MSM261S4030H0R的I2S数据

本文详细介绍了如何使用Arduino IDE读取MSM261S4030H0R数字麦克风的I2S数据,涵盖硬件连接、代码编写和数据可视化全流程。重点解决I2S引脚配置、环境噪声过滤和声波观察等实际问题,适合ESP32-S3开发者快速上手数字麦克风应用。

weixin_26742753的博客 314

Cracking the coding interview--Q5.4

题目 原文: Explain what the following code does: ((n & (n-1)) == 0). 译文: 解释下面代码的作用:((n&(n-1))==0) 解答 分析:首先该代码是一个位运算的式子,两个相邻的数作按位与运算得到0,而当两个二进制的各位不同时,作按位与运算才会等于0,所以只有当n为2的整数次幕时,比它小1的数的二进制的各位才会与它的二进

cfanr 1384

国产大模型 coding plan 实战决策指南:GLM、Kimi、豆包、abab 四大模型分层选型与工程落地

大模型辅助编程(coding assistant)正从概念验证走向规模化工程落地,其核心已不再是‘能否生成代码’,而是‘如何在真实开发流程中精准赋能’。理解模型能力边界、匹配技术栈特征、构建分层工作流(L1行级补全/L2函数生成/L3模块重构),是实现稳定提效的关键。GLM系列以工程稳定性见长,适合代码审计与测试生成;Kimi-Max凭借200K超长上下文胜任深度重构与合规审查;豆包响应快、框架适配深,是IDE高频补全的理想选择;abab系列则在IoT固件等跨模态场景中展现独特价值。本文聚焦国产模型在金融、

weixin_32238157的博客 427

Cracking the coding interview--Q4

Chapter 4 | Trees and Graphs 4.1 Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ

763

IMU标定中的Allen方差

Allen方差 Allen方差是IMU标定任务中的常用工具。IMU标定可以分为确定性误差和随机误差,确定性误差包括:scale、bias、misalignment。随机误差则主要是高斯白噪声和bias随机游走。Allen方差主要用于标定随机误差。 Allen方差获取 Allen方差的计算方法如下: 保持IMU静止, 采集N个数据点。 将N个点定长分组,常用的交叠式Allen方差如下(n=3): 对于每个子集,计算均值后,计算相邻两组的方差,得到的时间-方差曲线即为Allen方差。 Alle

realjc的博客 5733

从压电传感器到示波器:手把手教你用CA3140搭建电荷放大器(含PCB布局与调零技巧)

本文详细介绍了如何使用CA3140运算放大器搭建专业级电荷放大器,从压电传感器信号特性分析到PCB布局与调零技巧,全程实战演示。重点解析了CA3140的低通滤波器设计、反馈网络参数计算及抗干扰增强方案,帮助电子爱好者高效实现微弱电荷信号的稳定转换与测量。

weixin_31355483的博客 596

cracking-the-coding-interview的核心秘密:如何用单元测试“定义”面试题?C++ Catch+Python unittest的TDD完整教程

cracking-the-coding-interview 是一个为《Cracking the Coding Interview》(CTCI)第 6 版提供 **C++ 与 Python 双语言解法** 的开源项目,其最大特色是:**每一道面试题的解法都配有完整的自动化单元测试**(C++ 端用 Catch 框架,Python 端用 unittest 框架),自动化测试覆盖率超过 90%。更妙的是

gitblog_00501的博客 317

SCADA|KingSCADA通过组合框选择修改变量的值

《SCADA项目中组合框控件的使用方法》 本文详细介绍了SCADA系统中组合框(下拉框)控件的应用方法。通过具体示例演示如何实现:当选择"雷工笔记"时变量值为100,选择"雷工杂谈"时为200,选择"小李食代"时为300的功能。主要实现步骤包括:1)创建项目并添加组合框控件;2)设置Items选项及对应数值;3)创建内存变量;4)使用GetCurrentIndex()和GetItemData()方法;5)在选项变化事件中编写脚本。该控件在SCADA

做有价值的事,积累有价值的笔记! 858

Cracking the coding interview--Q9

Chapter 9 | Sorting and Searching 9.1 You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. 插入排序,需要调整数

1020

Unreal5从入门到精通之一文搞懂Unreal的C++代码编译之谜

我们在开发Unreal项目时,如果时蓝图模板的项目,一般问题不大,因为当你修改完蓝图,点击编译Compile+保存Save,就可以继续开发了。这对于开发者来说也非常友好。但是,如果你使用的C++模板开发项目,代码编译一切都变得魔幻起来,IDE(VS/Rider)里面有编译,编辑器里面也有编译,有什么区别呢?我们来列举几个新手常碰到的问题。1.代码我明明新建了,编辑器里怎么看不到?2.昨天我新家的功能运行的好好的,今天怎么感觉还是老逻辑。或者昨天加的新代码,新蓝图不见了。

TxNet 1602

40、5G NR射频要求解析

本文详细解析了5G NR射频相关要求,包括参考灵敏度与动态范围的定义、接收机对干扰信号的抗扰性要求(如阻塞、邻道选择性等)、辐射射频要求在FR1和FR2频段的差异,以及多标准无线电基站(MSR)的发展及其对射频要求的影响。内容涵盖测试模型、干扰场景、具体指标和实现挑战,为理解和应用5G NR射频规范提供了全面指导。

qsc901234的博客 245

Cracking the coding interview--Q17

Chapter 17 | Networking 17.1 Explain what happens, step by step, after you type a URL into a browser. Use as much detail as possible.  先进行DNS的namequery,得到对方的cname或者a记录(当然也可能是IPV6),反正就最后得到对方的IP

630

麻辣香锅病毒分析

Spicy Hot Pot浏览器劫持病毒(麻辣香锅病毒)从2020年初被安全公司监测到,因其病毒模块带有MLXG_KM被安全行业代称麻辣香锅病毒。目前通过各种激活工具传播, 包括暴风激活、小马激活和KMS激活等诸多工具,疑似与下载站进行合作推广此类激活工具。Spicy Hot Pot 是一个浏览器劫持 Rootkit,它会将用户的主页更改为指向恶意软件操作员控制的页面,此外还会将内存转储从机器上传到预定义的服务器,并结合本地更新功能以确保其保持更新。通常浏览器劫持者会通过更改用户主页的恶意可执行文件或注册表

不忘初心,护天下安全! 4787

渣基础:比照Hawstein学Cracking the coding interview(1)

《C++ Primer 第五版》书实在是太长,太厚了。总是看了十几页就看累了,坚持不了多久,想了想还是别勉强自己,jued

低端玩家的记录地 949

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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

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

Cracking the coding interview--Q4.7

题目 原文: You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. 译文: 有两个非常大的二叉树:T1,有上百万个结点,T2,有上百个

cfanr 1452

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

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

Cracking the coding interview--Q12.5

题目 原文: If you were designing a web crawler, how would you avoid getting into infinite loops? 译文: 如果你正在设计一个网络爬虫,何如避免进入无限循环? 解答

cfanr 1630

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Cracking the coding interview--Q3.5

题目 原文: Implement a MyQueue class which implements a queue using two stacks. 译文: 用两个栈实现一个队列MyQueue。 解答 方法

cfanr 1448

Cracking the coding interview--Q2.5

Cracking the coding interview--Q2.5 December 17, 2012 作者:Hawstein 出处:http://hawstein.com/posts/2.5.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。 题目

pi9nc的专栏 817

Cracking the coding interview--Q3.6

原文: Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write thi

执子之手 560

Cracking the coding interview--Q9.3

题目 原文: Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log n) algorithm that finds an element in the array. You may assume that the array was originall

cfanr 1620

Cracking the coding interview--Q2.4

Cracking the coding interview--Q2.4 December 16, 2012 作者:Hawstein 出处:http://hawstein.com/posts/2.4.html 声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 ,转载请注明作者及出处。 题目 原文...

weixin_33985507的博客 75

Cracking the coding interview--Q5.7

题目 原文: An array A[1... n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements

cfanr 1649

程序员面试题目:Cracking the coding interview 分析与解答

Cracking the coding interview–Q11.2Cracking the coding interview–Q11.1Cracking the coding interview–Q10.6Cracking the coding interview–Q10.4Cracking the coding interview–Q10.2Cracking the

i 綠茶 1793

Cracking the coding interview--Q1.5

Cracking the coding interview--Q1.5 原文 Implement a method to perform basic string compression using the countsof repeated characters. For example, the string aabcccccaaa w...

weixin_34378045的博客 115

Cracking the coding interview--Q5

Chapter 5 | Bit Manipulation 5.1 You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a subst

951

Cracking the coding interview--Q9.6

原文: Given a matrix in which each row and each column is sorted, write a method to find an element in it. 译文: 给出一个矩阵,其中每一行和每一列都是有序的,写一个函数在矩阵中找出指定的数。 矩阵是有序的,因此要利用这一点,当矩阵中元素和要查找的元素对比后,

执子之手 604
上一篇: 数据结构:二叉树的非递归遍历
下一篇: Cracking the coding interview--Q4.6
java_wliang
博客等级 码龄15年 19粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值