首页 > 其他 > 详细

LeetCode 965. Univalued Binary Tree

时间:2019-05-19 14:18:29      阅读:106      评论:0      收藏:0      [点我收藏+]

965. Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value.

Return true if and only if the given tree is univalued.

Example 1:

技术分享图片
Input: [1,1,1,1,1,null,1]
Output: true

Example 2:

技术分享图片
Input: [2,2,2,5,2]
Output: false
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isUnivalTree(TreeNode* root) {
        if (root == nullptr)
            return true;
        if (root->left && root->left->val != root->val) return false;
        if (root->right && root->right->val != root->val) return false;
        
        return isUnivalTree(root->left) && isUnivalTree(root->right);
    }
};

 

 

 

 

 

LeetCode 965. Univalued Binary Tree

原文:https://www.cnblogs.com/douzujun/p/10889010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!