A binary tree is uni-valued if every node in the tree has the same value.
Given the root of a binary tree,returntrueif the given tree is uni-valued,orfalse otherwise.
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/univalued-binary-tree
The codes for above problem
classSolution{public:boolisUnivalTree(TreeNode* root){
vector<int> val;pre_order(root, val);for(int i =1; i < val.size(); i++){if(val[i]!= val[i-1]){returnfalse;}}returntrue;}voidpre_order(TreeNode* root, vector<int>& val){if(root==nullptr)return;
val.emplace_back(root->val);pre_order(root->left, val);pre_order(root->right, val);}};
The description of the problemA binary tree is uni-valued if every node in the tree has the same value.Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.来源:力扣(LeetCode)链接:https://leetcode.cn/problems/uni