0965.cpp (462B)
1 class Solution { 2 public: 3 bool isUnivalTree(TreeNode *root) { 4 if (!root) return true; 5 int val = root->val; 6 stack<TreeNode *> st; 7 8 st.push(root); 9 while (!st.empty()) { 10 TreeNode *root = st.top(); 11 st.pop(); 12 if (root->val != val) return false; 13 if (root->left) st.push(root->left); 14 if (root->right) st.push(root->right); 15 } 16 17 return true; 18 } 19 };