leetcode题解-965-单值二叉树 发表于 2019-12-30 | 分类于 树的遍历 题目 My way dfs遍历 12345678910111213141516171819202122232425262728293031323334/** * 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 {private: int val; bool isUT = true; void dfs(TreeNode* root) { if (!root || !isUT) return; isUT = (val == root->val ? true : false); dfs(root->left); dfs(root->right); }public: bool isUnivalTree(TreeNode* root) { val = root->val; dfs(root); return isUT; }};