Leetcode 199 Binary Tree Right Side View

本文介绍了一种算法问题的解决方案:给定一棵二叉树,返回从顶部到底部的右侧可见节点值。提供了两种方法,一种使用深度优先搜索(DFS),另一种使用广度优先搜索(BFS)。

Problem:

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.

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:

                                1            <-
                              /    \
                              2     3        <-  
                               \     \
                                5     4      <-

Analysis:

method 1:
dfs:
we search the tree with the order of {root, right, left}. We will find that each time we go from the upper level to next level, the first TreeNode we hit is the Node we are looking for.

method 2:
bfs:
When scanning the tree (left -> right) from one level to the next level.
The last node in this level is the node we are looking for.


// dfs
   public List<Integer> rightSideView(TreeNode root) {
	   
	   List<Integer> res = new ArrayList<>();
	   dfsHelper(res, root, 0);
	   return res;
    }
   public void dfsHelper(List<Integer> res, TreeNode root, int level) {
	   if (root == null) return;
	   
	   if (res.size() == level) {
		   res.add(root.val);
	   }
	   
	   dfsHelper(res, root.right, level+1);	   
	   dfsHelper(res, root.left, level+1);	
	   
   }
   
   
   
   // bfs
   public List<Integer> rightSideView1(TreeNode root){
	   
	  List<Integer> res = new ArrayList<>();
	  if (root == null) {
		  return res;
	  }
	  Queue<TreeNode> q = new LinkedList<>();
	  
	  q.offer(root);
	  while(!q.isEmpty()) {
		  int size = q.size();
		  for (int i=0; i<size; i++) {
			  TreeNode cur = q.poll();
			  
			  if (i == size-1) {
				  res.add(cur.val);
			  }
			  
			  if (cur.left != null) {
				  q.offer(cur.left);  
			  }
			  
			  if (cur.right != null) {
				  q.offer(cur.right);
			  }  
		  }	  
	  }
	    
	 return res;  
   }

内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值