C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
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:
TreeNode* searchBST(TreeNode* root, int val) {
}
};
运行代码
提交
golang 解法, 执行用时: 20 ms, 内存消耗: 7.2 MB, 提交时间: 2021-07-26 13:59:09
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return nil
}
if root.Val > val {
return searchBST(root.Left, val)
} else if root.Val < val {
return searchBST(root.Right, val)
} else {
return root
}
}
php 解法, 执行用时: 32 ms, 内存消耗: 17.2 MB, 提交时间: 2021-06-02 15:33:02
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($val = 0, $left = null, $right = null) {
* $this->val = $val;
* $this->left = $left;
* $this->right = $right;
* }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @param Integer $val
* @return TreeNode
*/
function searchBST($root, $val) {
while ( $root != null && $root->val != $val ) {
$root = $root->val > $val ? $root->left : $root->right;
}
return $root;
}
}
php 解法, 执行用时: 56 ms, 内存消耗: 17.3 MB, 提交时间: 2021-06-02 15:28:27
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($val = 0, $left = null, $right = null) {
* $this->val = $val;
* $this->left = $left;
* $this->right = $right;
* }
* }
*/
class Solution {
/**
* @param TreeNode $root
* @param Integer $val
* @return TreeNode
*/
function searchBST($root, $val) {
$queue = [$root];
while ( !empty($queue) ) {
$node = array_shift($queue);
if ( $node->val == $val ) return $node;
if ( $node->right )
$queue[] = $node->right;
if ( $node->left )
$queue[] = $node->left;
}
return null;
}
}