当前位置:网站首页>[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;
}
};
边栏推荐
- What is cloud development? Why cloud development? Talk about our story
- Tencent blue whale container management platform was officially released!
- Ps\ai and other design software pondering notes
- Snowflake algorithm implemented in go language
- How FEA and FEM work together
- [tke] nodelocaldnschache is used in IPVS forwarding mode
- Introduction of thread pool and sharing of practice cases
- B. Terry sequence (thinking + greed) codeforces round 665 (Div. 2)
- What is a framework?
- Transpose convolution learning notes
猜你喜欢

Some adventurer hybrid versions with potential safety hazards will be recalled

Cognition and difference of service number, subscription number, applet and enterprise number (enterprise wechat)

C. K-th not divisible by n (Mathematics + thinking) codeforces round 640 (Div. 4)

A survey on model compression for natural language processing (NLP model compression overview)
MySQL進階系列:鎖-InnoDB中鎖的情况
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel

There are potential safety hazards Land Rover recalls some hybrid vehicles

Applet wxss

Applet - use of template
MySQL Advanced Series: locks - locks in InnoDB
随机推荐
Data acquisition and transmission instrument reservoir dam safety monitoring
Tencent blue whale container management platform was officially released!
Leetcode notes of Google boss | necessary for school recruitment!
[tke] troubleshooting tips for container problems
Introduction to koa (III) koa routing
Go deep into the implementation principle of go language defer
C. K-th not divisible by n (Mathematics + thinking) codeforces round 640 (Div. 4)
Applet - use of template
D. Solve the maze (thinking +bfs) codeforces round 648 (Div. 2)
TRTC web end imitation Tencent conference microphone mute detection
山金期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
Load MySQL table data consumption quick installation configuration through kafka/flink
Istio FAQ: sidecar stop sequence
Kubernetes 1.20.5 setting up Sentinel
Where is the most formal and safe account opening for speculation futures? How to open a futures account?
Tencent on the other hand, I was puzzled by the "horse race" problem
Greenplum role-based fine-grained permission control
How to use the national standard streaming media server to view the video stream of the surveillance camera? How to correctly use UDP and TCP protocols?
Page scrolling effect library, a little skinny
Detailed explanation of transpose convolution in pytorch