C#
C++
Go
Java
Python
JavaScript
/**
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val, TreeNode left = null, TreeNode right = null)
* {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution
{
private static int GetLeftmostValue(TreeNode node)
{
while (node.left != null)
{
node = node.left;
}
return node.val;
}
private static int GetRightmostValue(TreeNode node)
{
while (node.right != null)
{
node = node.right;
}
return node.val;
}
public static bool ValidateTreeLeaves(TreeNode root, int minVal, int maxVal)
{
if (root == null)
{
return true;
}
int leftmost = GetLeftmostValue(root);
int rightmost = GetRightmostValue(root);
if (leftmost < minVal || leftmost > maxVal || rightmost < minVal || rightmost > maxVal)
{
return false;
}
return ValidateTreeLeaves(root.left, minVal, maxVal) &&
ValidateTreeLeaves(root.right, minVal, maxVal);
}
}
#include <vector>
using namespace std;
/**
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
int getLeftmostValue(TreeNode* node) {
while (node->left != nullptr) {
node = node->left;
}
return node->val;
}
int getRightmostValue(TreeNode* node) {
while (node->right != nullptr) {
node = node->right;
}
return node->val;
}
bool validateTreeLeaves(TreeNode* root, int minVal, int maxVal) {
if (root == nullptr) {
return true;
}
int leftmost = getLeftmostValue(root);
int rightmost = getRightmostValue(root);
if (leftmost < minVal || leftmost > maxVal || rightmost < minVal || rightmost > maxVal) {
return false;
}
return validateTreeLeaves(root->left, minVal, maxVal) &&
validateTreeLeaves(root->right, minVal, maxVal);
}
package main
// type TreeNode struct {
// Val int
// Left *TreeNode
// Right *TreeNode
// }
func getLeftmostValue(node *TreeNode) int {
for node.Left != nil {
node = node.Left
}
return node.Val
}
func getRightmostValue(node *TreeNode) int {
for node.Right != nil {
node = node.Right
}
return node.Val
}
func validateTreeLeaves(root *TreeNode, minVal int, maxVal int) bool {
if root == nil {
return true
}
leftmost := getLeftmostValue(root)
rightmost := getRightmostValue(root)
if leftmost < minVal || leftmost > maxVal || rightmost < minVal || rightmost > maxVal {
return false
}
return validateTreeLeaves(root.Left, minVal, maxVal) &&
validateTreeLeaves(root.Right, minVal, maxVal)
}
import java.util.*;
/**
* class TreeNode {
* Integer val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(Integer val) { this.val = val; }
* TreeNode(Integer val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean validateTreeLeaves(TreeNode root, int minVal, int maxVal) {
if (root == null) {
return true;
}
int leftmost = getLeftmostValue(root);
int rightmost = getRightmostValue(root);
if (leftmost < minVal || leftmost > maxVal || rightmost < minVal || rightmost > maxVal) {
return false;
}
return validateTreeLeaves(root.left, minVal, maxVal) &&
validateTreeLeaves(root.right, minVal, maxVal);
}
private int getLeftmostValue(TreeNode node) {
while (node.left != null) {
node = node.left;
}
return node.val;
}
private int getRightmostValue(TreeNode node) {
while (node.right != null) {
node = node.right;
}
return node.val;
}
}
from tree_node import TreeNode
# class TreeNode:
# def __init__(self, val, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def validate_tree_leaves(root: TreeNode, min_val: int, max_val: int) -> bool:
def get_leftmost_value(node):
while node.left:
node = node.left
return node.val
def get_rightmost_value(node):
while node.right:
node = node.right
return node.val
if not root:
return True
leftmost = get_leftmost_value(root)
rightmost = get_rightmost_value(root)
if leftmost < min_val or leftmost > max_val or rightmost < min_val or rightmost > max_val:
return False
return validate_tree_leaves(root.left, min_val, max_val) and \
validate_tree_leaves(root.right, min_val, max_val)
import { TreeNode } from './treeNode.js';
/**
* class TreeNode {
* constructor(val, left, right) {
* this.val = val;
* this.left = (left === undefined ? null : left);
* this.right = (right === undefined ? null : right);
* }
* }
*/
/*
class TreeNode {
constructor(value) {
this.val = value;
this.left = null;
this.right = null;
}
}
*/
export function validateTreeLeaves(root, minVal, maxVal) {
function getLeftmostValue(node) {
while (node.left) {
node = node.left;
}
return node.val;
}
function getRightmostValue(node) {
while (node.right) {
node = node.right;
}
return node.val;
}
if (!root) {
return true;
}
const leftmost = getLeftmostValue(root);
const rightmost = getRightmostValue(root);
if (leftmost < minVal || leftmost > maxVal || rightmost < minVal || rightmost > maxVal) {
return false;
}
return validateTreeLeaves(root.left, minVal, maxVal) &&
validateTreeLeaves(root.right, minVal, maxVal);
}
Полный обход всех вершин дерева не является оптимальным решением и не будет принят интервьюером. Нужно воспользоваться свойством бинарного дерева: самый левый узел — самый маленький, а самый правый — самый большой. Проверки этих двух узлов достаточно.