Remove Node in Binary Search Tree

本文介绍了一种Java实现二叉树节点删除的方法,包括四种情况:节点为叶子节点、只有一侧子节点、两侧都有子节点,以及如何在特定节点被删除后找到并替换它的值。

This is one of the pretty interesting technical interview questions I see, it is rather fundamental, you probably have seen similar from your text book. But you know what, in the real interview process, few people can actually implement the code correctly, around 10% graduates from top CS schools, let's put it that way.


The following is my java implementation:


 public TreeNode remove(TreeNode root, int target){
	    	if(root==null)
	    		return root;
	    	if(target<root.data)
	    		root.left = remove(root.left,target);
	    	else if(target>root.data)
	    		root.right = remove(root.right,target);
	    	//now we find this node, root.data = target, just need to delete this node
	    	else{
	    		// case 1, root is a leaf node
	    		if(root.left==null && root.right==null)
	    			root = null;
	    		// case 2, root has only left subtree
	    		else if(root.right==null)
	    			root = root.left;
	    		// case 3, root has only right subtree
	    		else if(root.left==null)
	    			root = root.right;
	    		// case 4 root has both left and right subtree
	    		else{
	    			//we can either find the largest value from left subtree or smallest from right subtree
	    			root.data = findSmallest(root.right);
	    			// delete that duplicate node from right sub tree
	    			remove(root.right,root.data);
	    		}
	    		
	    	}
	    	return root;
	    }
	    
	    public int findSmallest(TreeNode root){
	    	if(root==null) return 0;
	    	if(root.left==null)
	    		return root.data;
	    	else return findSmallest(root.left);
	    }


内容概要:本文系统研究了基于动态三维环境下的Q-Learning算法在无人机自主避障路径规划中的应用,依托Matlab代码实现,深入剖析了强化学习在复杂、时变空间中实现智能决策的机制。研究构建了三维网格化状态空间模型,设计了合理的动作集合与奖励函数,充分考虑静态与动态障碍物的存在,使无人机能够通过与环境持续交互,自主学习规避障碍并趋近目标的最优策略。文章不仅展示了Q-Learning算法在路径规划中的具体实现流程,还涵盖了状态表示、策略迭代、收敛性分析等关键环节,并通过仿真实验验证了算法的有效性与鲁棒性,为智能体在动态环境中的自主导航提供了理论依据和技术参考。; 适合人群:具备人工智能、自动化、计算机科学或机器人学等相关专业背景,熟悉Matlab编程语言和基本的强化学习概念,从事无人机控制、智能导航、路径规划算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于城市峡谷、灾害现场等复杂动态三维场景中无人机的自主飞行与紧急避障;②作为强化学习解决实际路径规划问题的教学实例,帮助理解Q-Learning的核心思想、状态-动作值函数更新过程及探索-利用权衡策略;③为后续研究更先进的深度强化学习算法(如DQN、PPO)在无人机控制中的应用奠定基础和提供对比基准。; 阅读建议:建议读者结合所提供的Matlab代码进行动手实践,通过调整学习率、折扣因子、探索率(ε-greedy)等超参数,观察其对算法收敛速度和最终路径规划质量的影响,并尝试修改环境复杂度(如增加障碍物密度或动态性)以评估算法的泛化能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值