C++
Java
Python
Python3
C
C#
JavaScript
TypeScript
PHP
Swift
Kotlin
Dart
Go
Ruby
Scala
Rust
Racket
Erlang
Elixir
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* 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:
vector<TreeNode*> splitBST(TreeNode* root, int target) {
}
};
运行代码
提交
golang 解法, 执行用时: 0 ms, 内存消耗: 2.1 MB, 提交时间: 2023-10-22 10:59:47
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func splitBST(root *TreeNode, V int) []*TreeNode {
out := []*TreeNode{nil, nil}
if nil == root {
return out
}
if root.Val <= V {
out = splitBST(root.Right, V)
root.Right, out[0] = out[0], root
} else {
out = splitBST(root.Left, V)
root.Left, out[1] = out[1], root
}
return out
}
cpp 解法, 执行用时: 4 ms, 内存消耗: 9 MB, 提交时间: 2023-10-22 10:59:23
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> splitBST(TreeNode* root, int V) { // [0]: small, [1]: big
if (!root) return { nullptr, nullptr };
if (root->val <= V) {
auto splitRight = splitBST(root->right, V);
root->right = splitRight[0]; // small
return { root, splitRight[1] };
} else {
auto splitLeft = splitBST(root->left, V);
root->left = splitLeft[1]; // big
return { splitLeft[0], root };
}
}
};
python3 解法, 执行用时: 44 ms, 内存消耗: 16.2 MB, 提交时间: 2023-10-22 10:59:02
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def splitBST(self, root: Optional[TreeNode], target: int) -> List[Optional[TreeNode]]:
if not root:
return None, None
elif root.val <= target:
bns = self.splitBST(root.right, target)
root.right = bns[0]
return root, bns[1]
else:
bns = self.splitBST(root.left, target)
root.left = bns[1]
return bns[0], root
java 解法, 执行用时: 0 ms, 内存消耗: 39.9 MB, 提交时间: 2023-10-22 10:58:32
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode[] splitBST(TreeNode root, int V) {
if (root == null)
return new TreeNode[]{null, null};
else if (root.val <= V) {
TreeNode[] bns = splitBST(root.right, V);
root.right = bns[0];
bns[0] = root;
return bns;
} else {
TreeNode[] bns = splitBST(root.left, V);
root.left = bns[1];
bns[1] = root;
return bns;
}
}
}