965. Univalued Binary Tree

LeetCode easy original: C# #csharp #easy #graph #leetcode #tree #two-pointers
Le texte du problème est traduit du russe pour la langue sélectionnée. Le code reste inchangé.

Бинарное arbre является одноценным, если каждый узел в дереве имеет одинаковое значение.

Дан корень бинарного дерева, return true, если данное arbre является одноценным, или false в противном случае.

Exemple:

Input: root = [1,1,1,1,1,null,1]

Output: true

C# solution

correspondant/original
public class Solution {
    private List<int> vals;
    public bool IsUnivalTree(TreeNode root) {
        vals = new List<int>();
        Dfs(root);
        foreach (int v in vals) {
            if (v != vals[0]) {
                return false;
            }
        }
        return true;
    }
    private void Dfs(TreeNode node) {
        if (node != null) {
            vals.Add(node.val);
            Dfs(node.left);
            Dfs(node.right);
        }
    }
}

C++ solution

brouillon automatique, à relire avant soumission
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    private List<int> vals;
    public bool IsUnivalTree(TreeNode root) {
        vals = new List<int>();
        Dfs(root);
        foreach (int v in vals) {
            if (v != vals[0]) {
                return false;
            }
        }
        return true;
    }
    private void Dfs(TreeNode node) {
        if (node != null) {
            vals.push_back(node.val);
            Dfs(node.left);
            Dfs(node.right);
        }
    }
}

Java solution

brouillon automatique, à relire avant soumission
import java.util.*;
import java.math.*;

// Auto-generated Java draft from the C# solution. Review API differences before LeetCode submit.
public class Solution {
    private List<int> vals;
    public boolean IsUnivalTree(TreeNode root) {
        vals = new List<int>();
        Dfs(root);
        foreach (int v in vals) {
            if (v != vals[0]) {
                return false;
            }
        }
        return true;
    }
    private void Dfs(TreeNode node) {
        if (node != null) {
            vals.add(node.val);
            Dfs(node.left);
            Dfs(node.right);
        }
    }
}

Python solution

correspondant/original
class Solution:
    def isUnivalTree(self, root: TreeNode) -> bool:
        vals = []
        
        def dfs(node):
            if node:
                vals.append(node.val)
                dfs(node.left)
                dfs(node.right)
        
        dfs(root)
        return all(v == vals[0] for v in vals)

Go solution

correspondant/original
func isUnivalTree(root *TreeNode) bool {
    vals := []int{}
    
    var dfs func(node *TreeNode)
    dfs = func(node *TreeNode) {
        if node != nil {
            vals = append(vals, node.Val)
            dfs(node.Left)
            dfs(node.Right)
        }
    }
    
    dfs(root)
    for _, v := range vals {
        if v != vals[0] {
            return false
        }
    }
    return true
}

Algorithm

Выполните обход дерева в глубину (DFS), чтобы собрать все значения узлов в список.

Проверьте, что все значения в списке одинаковы.

Если все значения одинаковы, return true, иначе return false.

😎

Vacancies for this task

offres actives with overlapping task tags are affichés.

Toutes les offres
Il n'y a pas encore d'offres actives.