首页 > 其他分享 >LeetCode 1382. Balance a Binary Search Tree

LeetCode 1382. Balance a Binary Search Tree

时间:2022-09-25 15:14:37浏览次数:81  
标签:Binary Search TreeNode val tree Tree return null root

原题链接在这里:https://leetcode.com/problems/balance-a-binary-search-tree/

题目:

Given the root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any of them.

A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than 1.

Example 1:

Input: root = [1,null,2,null,3,null,4,null,null]
Output: [2,1,3,null,null,null,4]
Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct.

Example 2:

Input: root = [2,1,3]
Output: [2,1,3] 

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • 1 <= Node.val <= 105

题解:

Could perform a inorder traverse first and then construst balanced BST from inorder list.

TimeComplexity: O(n). n is total number of numbers in the tree.

Space: O(n). list size.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     List<TreeNode> list = new ArrayList<>();
18     public TreeNode balanceBST(TreeNode root) {
19         inorderDfs(root);
20         return constructBalancedBst(0, list.size() - 1);
21     }
22     
23     private void inorderDfs(TreeNode root){
24         if(root == null){
25             return;
26         }
27         
28         inorderDfs(root.left);
29         list.add(root);
30         inorderDfs(root.right);
31     }
32     
33     private TreeNode constructBalancedBst(int l, int r){
34         if(l > r){
35             return null;
36         }
37         
38         int mid = l + (r - l) / 2;
39         TreeNode root = list.get(mid);
40         root.left = constructBalancedBst(l, mid - 1);
41         root.right = constructBalancedBst(mid + 1, r);
42         return root;
43     }
44 }

类似Convert Sorted Array to Binary Search Tree.

标签:Binary,Search,TreeNode,val,tree,Tree,return,null,root
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16727874.html

相关文章

  • CF804D Expected diameter of a tree(期望+根号分治)
    tag期望,根号分治。大致题意:给你一个森林,每次询问两个点,求把两个点所在联通块连接起来生成的树的直径的期望。分析:如果是期望的话,只需要求出所有可能情况下的能生成的......
  • leetcode 669. Trim a Binary Search Tree 修剪二叉搜索树 (简单)
    一、题目大意给你二叉搜索树的根节点root,同时给定最小边界low和最大边界high。通过修剪二叉搜索树,使得所有节点的值在[low,high]中。修剪树不应该改变保留在树中的......
  • ABC 243 D - Moves on Binary Tree(树+字符串)
    https://atcoder.jp/contests/abc243/tasks/abc243_d题目大意:给定一颗完全二叉树,他总共可以有(2^10^100)-1个节点,节点下标为1,2,...,(2^10^100)-1。给我们一个长度为n......
  • ElasticSearch
    (36条消息)ElasticSearch高级(QueryDSL查询bulk批量操作导入数据各种查询实战技巧-优化比重全量与增量数据同步)_Ybb_studyRecord的博客-CSDN博客_elasticsearch批......
  • 《Elasticsearch深入浅出》一、elasticsearch安装上手
    Elasticsearch是位于ElasticStack核心的分布式搜索和分析引擎。Elasticsearch为所有类型的数据提供近乎实时的搜索和分析。无论您拥有结构化或非结构化文本、数字数......
  • monstache 实时同步mongodb 数据到 elasticsearch
    最近在做数据统计功能,需要将mongodb数据实时同步到 elasticsearch中。目前找到的方案有两种1、通过flinkmongodbcdc flinkmongodbcdc的优点是比较灵活,可以将mong......
  • TrieTree(字典树)
    TrieTree(字典树)定义TrieTree,,字典树,又叫前缀树,单词查找树,是一种针对字符串前缀进行维护的数据结构,给定一个字符串集合构建的前缀树,可以在树中查找字符串或者字符串的......
  • elasticsearch遇到的一个小bug
     报错如图,idea里面只显示一些较为粗略的异常 经查询排错,原来是这里的score写成content了,Text类型不能排序  ......
  • Roadside Trees (Simplified Edition) CodeForces - 265B
    RoadsideTrees(SimplifiedEdition)CodeForces-265B松鼠Liss喜欢坚果。一条街上有n棵树(从西到东编号为1到n),每棵树的顶部都有一颗美味的坚果。树的高度我很高。......
  • leetcode 144. Binary Tree Preorder Traversal 二叉树展开为链表(中等)
    一、题目大意给你二叉树的根节点root,返回它节点值的前序遍历。示例1:输入:root=[1,null,2,3]输出:[1,2,3]示例2:输入:root=[]输出:[]示例3:输入:root=......