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 };

 

posted @ 2018-12-30 16:03  Asurudo  阅读(333)  评论(0编辑  收藏  举报