[US Giants] 七. Binary Tree

本文详细介绍了二叉树的各种操作,包括平衡二叉树的判定、插入节点到二叉搜索树、利用前序与中序遍历构建二叉树、通过中序与后序遍历构建二叉树以及在二叉搜索树中移除节点等核心算法。
Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example

Given binary tree A = {3,9,20,#,#,15,7}, B = {3,#,20,15,7}

A)  3            B)    3 
   / \                  \
  9  20                 20
    /  \                / \
   15   7              15  7

The binary tree A is a height-balanced binary tree, but B is not.

思路:如果左右subtree有一个不平衡,就不符合要求

         如果左右subtree都是平衡的,但是要符合两边高度差不能大于1

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: True if this Binary tree is Balanced, or false.
     */
    
    public boolean isBalanced(TreeNode root) {
        return helper(root)!=-1;
    }
    
    private int helper(TreeNode root){
        if(root==null){
            return 0;
        }
     
        int leftDepth=helper(root.left);
        int rightDepth=helper(root.right);
        if(leftDepth==-1 || rightDepth==-1 || Math.abs(leftDepth-rightDepth)>1){
            return -1;
        }
        return Math.max(leftDepth,rightDepth)+1;
    }
}
Insert Node in a Binary Search Tree

Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.

You can assume there is no duplicate values in this tree + node.

Example

Given binary search tree as follow, after Insert node 6, the tree should be:

  2             2
 / \           / \
1   4   -->   1   4
   /             / \ 
  3             3   6
Challenge Can you do it without recursion?
思路:由于是BST,而且题目给出条件tree+node里没有重复值,因此node的去向就是两种,要么root左边,要么root右边

         如果node的值比当前root值小,去向就是左边,继续找,如果左边值不为null,继续判断,如果为null,就直接赋值node为这个位置

         如果node的值比当前root值大,去向就是右边,继续找,如果右边值不为null,继续判断,如果为null,就直接赋值node为这个位置       

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {                                         //non-recursion
    /**
     * @param root: The root of the binary search tree.
     * @param node: insert this node into the binary search tree
     * @return: The root of the new binary search tree.
     */
    public TreeNode insertNode(TreeNode root, TreeNode node) {
        if(root==null){
            return node;
        }
        
        TreeNode cur=root;
        while(true){                                           //因为while(true)是一个无限循环,表达式一直为真
            if(node.val<cur.val){                              //需要break跳出循环
                if(cur.left!=null){
                   cur=cur.left; 
                }else{
                   cur.left=node;
                   break;
                }
            }else if(node.val>cur.val){
                if(cur.right!=null){
                   cur=cur.right; 
                }else{
                   cur.right=node;
                   break;
                }
            }
        }
        return root;
    }
}

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {                                         //recursion
    /**
     * @param root: The root of the binary search tree.
     * @param node: insert this node into the binary search tree
     * @return: The root of the new binary search tree.
     */
    public TreeNode insertNode(TreeNode root, TreeNode node) {
        if(root==null){
            return node;                                        //这里写成root=node是一样的
        }
        if(node.val<root.val){
            root.left=insertNode(root.left, node);
        }else{                                                  //因为题目指出没有duplicates,因此else的情况就是
            root.right=insertNode(root.right, node);            //node.val>root.val
        }
        return root;
    }
}
Construct Binary Tree from Preorder and Inorder Traversal

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

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

Example

Given in-order [1,2,3] and pre-order [2,1,3], return a tree:

  2
 / \
1   3
思路:preorder的第一个元素就是要建立的二叉树的root,在inorder数组里找到root的位置,也就是值等于prorder[pstart]的位置,记为pos

         pos的前面的就是左子树,pos的后面的就是右子树

注意:左右子树递归的时候,找preorder开始和结束位置的边界的时候,长度可以由inorder的长度来计算

         例如root.left=build(inoder,istart,pos-1,preorder,pstart+1,end);

         end值的计算:根据inorder部分计算左子树占据的数组长度,pos-1-istart+1=pos-istart

         因此preorder这部分的长度也是pos-istart,又因为preorder从pstart开始,所以最后知end为pstart+pos-istart

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
 
 
public class Solution {
    /**
     *@param preorder : A list of integers that preorder traversal of a tree
     *@param inorder : A list of integers that inorder traversal of a tree
     *@return : Root of a tree
     */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder==null || inorder==null){
            return null;
        }
        if(preorder.length!=inorder.length){
            return null;
        }
        return build(inorder,0,inorder.length-1,preorder,0,preorder.length-1);      
    }
    
    private TreeNode build(int[] inorder,int istart,int iend,int[] preorder,int pstart,int pend){
        if(istart>iend){                                                         //这边写成pstart<pend也是一样的
            return null;
        }
        int pos=findPosition(inorder,istart,iend,preorder[pstart]);
        TreeNode root=new TreeNode(preorder[pstart]);
        root.left=build(inorder,istart,pos-1,preorder,pstart+1,pstart+pos-istart); 
        root.right=build(inorder,pos+1,iend,preorder,pstart+pos-istart+1,pend);  //preorder的右边开始位置是
        return root;                                                             //preorder左边结束位置+1
    } 
     
    private int findPosition(int[] arr,int start,int end,int pos){
        for(int i=start;i<=end;i++){
            if(arr[i]==pos){
                return i;
            }
        }
        return -1;
    }
}
Construct  Binary Tree from inorder and postorder traversal

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

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

Example

Given inorder [1,2,3] and postorder [1,3,2], return a tree:

  2
 / \
1   3

思路:和inorder,preorder建树的思路一样

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder.length!=postorder.length){
            return null;
        }
        return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
    }
    
    public TreeNode build(int[] inorder,int istart,int iend, int[] postorder,int pstart,int pend) {
        if(istart>iend){
            return null;
        }
        TreeNode root=new TreeNode(postorder[pend]);
        int pos=finPos(inorder,istart,iend,postorder[pend]);
        root.left=build(inorder,istart,pos-1,postorder,pstart,pstart+pos-istart-1);
        root.right=build(inorder,pos+1,iend,postorder,pstart+pos-istart,pend-1);
        return root;
    }
    
    private int finPos(int[] arr,int start,int end,int pos){
        for(int i=start;i<=end;i++){
            if(arr[i]==pos){
                return i;
            }
        }
        return -1;
    }
}
Remove Node in Binary Search Tree

Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still a binary search tree after removal.

Example

Given binary search tree:

    5
   / \
  3   6
 / \
2   4            当前root下有两个结点,找右面最小值,此时为4,把4赋值到当前root,再对4进行递归,此时value=4,下面没有结点,返回null

Remove 3, you can either return:

    5
   / \
  2   6
   \
    4

or

    5
   / \
  4   6
 /
2                本题解法就是最后建立这样一颗树
思路:考虑到三种情况:

         要删除的node下面没有结点,直接把此结点设为null

         要删除的node下面只有一个或左或右的结点,或者是说只有左子树或者只有右子树,直接返回这颗树

         要删除的node下面有两个结点,找到右子树的最小值结点,根据BST知,最小值结点一定在左面,或者就是它自己,

         把找到的最小值赋值到当前node的值,

         对于当前node的右子树再进行同样的递归,此时要递归删除的value就是刚才找到的右子树的最小值点

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of the binary search tree.
     * @param value: Remove the node with given value.
     * @return: The root of the binary search tree after removal.
     */
    public TreeNode removeNode(TreeNode root, int value) {
        if(root==null){
            return null;
        }
        
        if(value>root.val){
            root.right=removeNode(root.right, value);
        }else if(value<root.val){
            root.left=removeNode(root.left, value);
        }else{
            if(root.left==null && root.right==null){
                return null;
            }
            if(root.left==null){
                return root.right;
            }
            if(root.right==null){
                return root.left;
            }
            TreeNode minRight=findNode(root.right);
            root.val=minRight.val;
            root.right=removeNode(root.right,minRight.val);       //此时要递归的value就是刚才找到的右子树最小值
        }
        return root;
    }
    
    private TreeNode findNode(TreeNode cur){
        while(cur.left!=null){
            cur=cur.left;
        }
        return cur;
    }
}






内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
根据原作 https://pan.quark.cn/s/23d6270309e5 的源码改编 湖北省黄石市2021年中考数学试卷所包含的知识点广泛涉及了中学数学的基础领域,涵盖了实数、科学记数法、分式方程、几何体的三视图、立体几何、概率统计以及代数方程等多个方面。 接下来将对每道试题所关联的知识点进行深入剖析:1. 实数与倒数的定义:该题目旨在检验学生对倒数概念的掌握程度,即一个数a的倒数表达为1/a,因此-7的倒数可表示为-1/7。 2. 科学记数法的运用:科学记数法是一种表示极大或极小数字的方法,其形式为a×10^n,其中1≤|a|<10,n为整数。 此题要求学生运用科学记数法表示一个天文单位的距离,将1.4960亿千米转换为1.4960×10^8千米。 3. 分式方程的求解方法:考察学生解决包含分母的方程的能力,题目要求找出满足方程3/(2x-1)=1的x值,需通过消除分母的方式转化为整式方程进行解答。 4. 三视图的辨认:该题目测试学生对于几何体三视图(主视图、左视图、俯视图)的认识,需要识别出具有两个相同视图而另一个不同的几何体。 5. 立体几何与表面积的计算:题目要求学生计算由直角三角形旋转形成的圆锥的表面积,要求学生对圆锥的底面积和侧面积公式有所了解并加以运用。 6. 统计学的基础概念:题目涉及众数、平均数、极差和中位数的定义,要求学生根据提供的数据信息选择恰当的统计量。 7. 方程的整数解求解:考察学生在实际问题中进行数学建模的能力,通过建立方程来计算在特定条件下帐篷的搭建方案数量。 8. 三角学的实际应用:题目通过在直角三角形中运用三角函数来求解特定线段的长度。 利用正弦定理求解AD的长度是解答该问题的关键。 9. 几何变换的应用:题目要求学生运用三角板的旋转来求解特定点的...
Python基于改进粒子群IPSO与LSTM的短期电力负荷预测研究内容概要:本文围绕“Python基于改进粒子群IPSO与LSTM的短期电力负荷预测研究”展开,提出了一种结合改进粒子群优化算法(IPSO)与长短期记忆网络(LSTM)的混合预测模型。通过IPSO算法优化LSTM网络的关键参数(如学习率、隐层节点数等),有效提升了模型在短期电力负荷预测中的精度与收敛速度。文中详细阐述了IPSO算法的改进策略(如引入自适应惯性权重、变异机制等),增强了全局搜索能力与避免早熟收敛,并利用实际电力负荷数据进行实验验证,结果表明该IPSO-LSTM模型相较于传统LSTM、PSO-LSTM等方法在预测准确性(如MAE、RMSE指标)方面表现更优。研究为电力系统调度、能源管理提供了高精度的负荷预测技术支持。; 适合人群:具备一定Python编程基础、熟悉基本机器学习算法的高校研究生、科研人员及电力系统相关领域的技术人员,尤其适合从事负荷预测、智能优化算法应用研究的专业人士。; 使用场景及目标:①应用于短期电力负荷预测,提升电网调度的精确性与稳定性;②为优化算法(如粒子群算法)与深度学习模型(如LSTM)的融合应用提供实践案例;③可用于学术研究、毕业论文复现或电力企业智能化改造的技术参考。; 阅读建议:建议读者结合文中提到的IPSO与LSTM原理进行理论学习,重点关注参数优化机制的设计思路,并动手复现实验部分,通过对比不同模型的预测结果加深理解。同时可拓展尝试将该方法应用于其他时序预测场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值