Leetcode-965 Univalued Binary Tree(单值二叉树)
1 class Solution 2 { 3 public: 4 bool PreOrderTraverse (TreeNode* T,int data) 5 { 6 if(T) 7 { 8 if(T->val!=data) 9 return false; 10 if(!PreOrderTraverse (T -> left,data)) 11 return false; 12 if(!PreOrderTraverse (T -> right,data)) 13 return false ; 14 } 15 return true; 16 } 17 bool isUnivalTree(TreeNode* root) 18 { 19 return PreOrderTraverse(root,root->val); 20 } 21 };