当前位置:网站首页>[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
2022-06-24 16:38:00 【Mountain peak evening view】
One 、 subject


Two 、 Ideas
Given ascending array , In fact, that is BST Traversal array in the middle order of , Just given a binary tree's middle order traversal array , It is not certain that a binary tree , But the problem requires a strictly balanced binary search tree , Therefore, you can select the middle element of the ascending sequence as the current root node element .
3、 ... and 、 Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return dfs(nums, 0, nums.size() - 1);
}
TreeNode* dfs(vector<int>& nums, int left, int right){
if(left > right){
return nullptr;
}
// Take the middle element of the ascending array as the root node root
int mid = left + (right - left) / 2;
TreeNode* root = new TreeNode(nums[mid]);
root->left = dfs(nums, left, mid - 1);
root->right = dfs(nums, mid + 1, right);
return root;
}
};
边栏推荐
- @There is a free copyright protection service for enterprises in Dawan District
- Private domain defense in the cotton Era
- C. Three displays codeforces round 485 (Div. 2)
- Serial of H3CNE experiment column - spanning tree STP configuration experiment
- CDs view permission check
- MySQL timestamp format conversion date format string
- [tke] enable CPU static management strategy
- Introduction to koa (III) koa routing
- 转置卷积学习笔记
- Applet - use of template
猜你喜欢

Applet - use of template
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel

Ps\ai and other design software pondering notes

Applet wxss

A survey of training on graphs: taxonomy, methods, and Applications

A survey on model compression for natural language processing (NLP model compression overview)

C. K-th not divisible by n (Mathematics + thinking) codeforces round 640 (Div. 4)
Advanced programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization

There are potential safety hazards Land Rover recalls some hybrid vehicles
MySQL Advanced Series: Locks - Locks in InnoDB
随机推荐
Bitwise Operators
Applet - use of template
Page scrolling effect library, a little skinny
Fastjson vulnerability utilization techniques
ZOJ - 4104 sequence in the pocket
How to open a futures account safely? Which futures companies are more reliable?
Regular expression learning artifact!
Yuanqi forest started from 0 sugar and fell at 0 sugar
A survey on model compression for natural language processing (NLP model compression overview)
Go deep into the implementation principle of go language defer
Handling of communication failure between kuberbetes pod
What is browser fingerprint recognition?
What can Lu yuanjiu Jiao buy?
Popular explanation [redirection] and its practice
Advanced programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization
Teach you to write a classic dodge game
[tke] troubleshooting tips for container problems
Introduction to koa (III) koa routing
MySQL timestamp format conversion date format string
[tke] enable CPU static management strategy