• LeetCode 108. 将有序数组转换为二叉搜索树


    class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            //二叉搜索树(又称二叉排序树) 的中序遍历 可得到一个升序序列,又平衡二叉树 中所有节点的左右子树的高度差绝对值不超过1.
            //根据这个升序序列构建平衡二叉树,根节点为序列的中心元素,(仅由中序序列不能唯一确定一棵树)
            return buildTree(nums,0,nums.length-1);
        }
        private TreeNode buildTree(int[] nums,int low,int high){
            if(low > high) return null;
            //根据序列长,得到序列的中心节点下标
            int mid = low + (high - low)/2;
            TreeNode root = new TreeNode(nums[mid]);
            //根据根节点构建左右子树
            root.left = buildTree(nums,low,mid-1);
            root.right = buildTree(nums,mid+1,high);
            //完成
            return root;
    
        }
    }
  • 相关阅读:
    合并字符串中的多个空格
    IfcSpecularRoughness
    IfcSpecularExponent
    IfcPresentableText
    IfcFontWeight
    IfcFontVariant
    uwb ifc模型定位測試
    IfcFontStyle
    IfcGeometricModelResource
    qt6安装
  • 原文地址:https://www.cnblogs.com/peanut-zh/p/13886126.html
Copyright © 2020-2023  润新知