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:
int goodNodes(TreeNode* root) {
}
};
运行代码
提交
php 解法, 执行用时: 72 ms, 内存消耗: 33.6 MB, 提交时间: 2023-08-25 07:41:48
/**
* 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
* @return Integer
*/
function goodNodes($root) {
return $this->dfs($root, PHP_INT_MIN);
}
function dfs($root, $path_max) {
if ( $root == null ) return 0;
$res = 0;
if ( $root->val >= $path_max ) {
$res++;
$path_max = $root->val;
}
$res += $this->dfs($root->left, $path_max) + $this->dfs($root->right, $path_max);
return $res;
}
}
javascript 解法, 执行用时: 124 ms, 内存消耗: 63.5 MB, 提交时间: 2023-08-25 07:37:28
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var goodNodes = function(root) {
const dfs = (root, path_max) => {
if (root == null) {
return 0;
}
let res = 0;
if (root.val >= path_max) {
res++;
path_max = root.val;
}
res += dfs(root.left, path_max) + dfs(root.right, path_max);
return res;
}
return dfs(root, -Infinity);
};
golang 解法, 执行用时: 80 ms, 内存消耗: 18.4 MB, 提交时间: 2023-08-25 07:36:22
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func goodNodes(root *TreeNode) int {
return dfs(root, math.MinInt)
}
func dfs(root *TreeNode, path_max int) int {
if root == nil {
return 0
}
res := 0
if root.Val >= path_max {
res++
path_max = root.Val
}
res += dfs(root.Left, path_max) + dfs(root.Right, path_max)
return res
}
cpp 解法, 执行用时: 124 ms, 内存消耗: 84.3 MB, 提交时间: 2022-11-19 20:15:40
/**
* 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:
int ans;
void dfs(TreeNode *root,int mymax)
{
if(root==nullptr)
return ;
if(root->val>=mymax)
ans++;
dfs(root->left,max(mymax,root->val));
dfs(root->right,max(mymax,root->val));
}
int goodNodes(TreeNode* root)
{
dfs(root,-2147483648);
return ans;
}
};
java 解法, 执行用时: 2 ms, 内存消耗: 49.5 MB, 提交时间: 2022-11-19 20:14:20
/**
* 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 int goodNodes(TreeNode root) {
if (root == null) return 0;
dfs(root, root.val);
return ans;
}
int ans = 0;
void dfs(TreeNode node, int curMax) {
if (node == null) return;
if (node.val >= curMax) {
ans++;
curMax = node.val;
}
dfs(node.left, curMax);
dfs(node.right, curMax);
}
}
java 解法, 执行用时: 2 ms, 内存消耗: 49.5 MB, 提交时间: 2022-11-19 20:14:02
/**
* 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 int goodNodes(TreeNode root) {
return this.goodNodes(root, root.val);
}
private int goodNodes(TreeNode root, int max) {
if (root == null) {
return 0;
}
int ans = root.val >= max ? 1 : 0;
max = Math.max(max, root.val);
ans += this.goodNodes(root.left, max);
ans += this.goodNodes(root.right, max);
return ans;
}
}
python3 解法, 执行用时: 236 ms, 内存消耗: 33.3 MB, 提交时间: 2022-11-19 20:13:24
# 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 goodNodes(self, root: TreeNode) -> int:
ans = 1
def dfs(u: TreeNode, Max: int) -> None:
nonlocal ans
if u.val >= Max:
ans += 1
if u.left:
dfs(u.left, max(Max, u.val))
if u.right:
dfs(u.right, max(Max, u.val))
if root.left:
dfs(root.left, root.val)
if root.right:
dfs(root.right, root.val)
return ans