Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

有序数组变二叉平衡搜索树,不难,递归就行。每次先序建立根节点(取最中间的数),然后用子区间划分左右子树。

一次就AC了

注意:new 结构体的时候对于

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

要用 TreeNode * root = new TreeNode(123);  与构造函数的形式要一致。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; //Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &num) {
if(num.empty())
{
return NULL;
} int nr = num.size()/;
TreeNode * root = new TreeNode(num[nr]); vector<int> numl(num.begin(), num.begin() + nr);
vector<int> numr(num.begin() + nr + , num.end());
root->left = sortedArrayToBST(numl);
root->right = sortedArrayToBST(numr); return root;
}
}; int main()
{
Solution s;
vector<int> num;
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back(); TreeNode * ans = s.sortedArrayToBST(num); return ;
}

【leetcode】Convert Sorted Array to Binary Search Tree (easy)的更多相关文章

  1. 【leetcode】Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  2. 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路: ...

  3. 【leetcode】Convert Sorted List to Binary Search Tree

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  4. 【Leetcode】【Medium】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...

  5. 【树】Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. 【leetcode】Convert Sorted List to Binary Search Tree (middle)

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

  8. [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  9. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

随机推荐

  1. 采用ETL with RDBMS模式来实现ETL

    目前Teradata数据仓库的ETL作业采用ELT方式, 因为loading太重了, 需要将ETL压力转移到专门的ETL Server上. 对于ETL工具, 市场上已有很成熟的商业/开源工具, 比如I ...

  2. 腾讯web前端笔试题及个人答案

    每道题都有答案,大多数答案亲测正确. 简答题 1.js中“5”+4=? 答案:54 2.js中void(0)=? 答案:undefined 3.js中NaN*4=? 答案:NaN 4.js中null* ...

  3. 黄学长模拟day1 球的序列

    N个编号为1-n的球,每个球都有唯一的编号.这些球被排成两种序列,分别为A.B序列,现在需要重新寻找一个球的序列l,对于这个子序列l中任意的两个球,要求j,k(j<k),都要求满足lj在A中位置 ...

  4. CHAP算法C++实现

    CHAP是一种挑战响应式协议. CHAP全称为:Challenge Handshake Authentication Protocol. CHAP密码 = 一个字节的标识符 + MD5(一个字节的标识 ...

  5. 类调用类的protected或private的成员函数或成员变量

    1.在其中一个类定义友元函数,则可以实现该类直接使用另外类的里所有内容. 一般实例化两个类,友元类以及自身类,实现友元类传递指针到自身类 2.如果两个类是可以继承的关系,则在子类里继承该类,实现在子类 ...

  6. Javascript高级程序设计——基本概念(二)

    相等操作符: 相等==:这个操作符会先转换操作数,强制类型转换,然后再比较他们的相等性. null == undefined //true NaN == NaN //false"5" ...

  7. 解决document.onclick在IE下用不了或无效的问题

    document.onclick这个事件在IE这几天突然下不能用了,导致JS代码部分有BUG document.onmouseover;document.onmouseout;document.onm ...

  8. CentOS-6.5-NFS部署

                      nfs-server与nfs-client端配置一样   NFS(network file system)网络文件系统:用于在网络上共享存储. 服务端-192.16 ...

  9. window.location.href 和 window.location.replace 的区别

    window.location.href  和  window.location.replace 的区别 1.window.location.href=“url”:改变url地址: 2.window. ...

  10. leetcode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...