Skip to content

Latest commit

 

History

History
35 lines (29 loc) · 789 Bytes

Question_965.md

File metadata and controls

35 lines (29 loc) · 789 Bytes

LeetCode Records - Question 965 Univalued Binary Tree

Attempt 1: Use recursion

class Solution {
    public boolean isUnivalTree(TreeNode root) {
        if (root == null) {
            return true;
        }

        return isUnivalTreeRecursion(root, root.val);
    }

    private boolean isUnivalTreeRecursion(TreeNode root, int val) {
        if (root == null) {
            return true;
        } else if (root.val != val) {
            return false;
        }

        if (!isUnivalTreeRecursion(root.left, val)) {
            return false;
        }
        if (!isUnivalTreeRecursion(root.right, val)) {
            return false;
        }

        return true;
    }
}
  • Runtime: 0 ms (Beats: 100.00%)
  • Memory: 40.90 MB (Beats: 72.89%)