same tree
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSameTree(TreeNode *p, TreeNode *q) { 13 // Start typing your C/C++ solution below 14 // DO NOT write int main() function 15 if ((p!= NULL) && (q!=NULL)) 16 { 17 if (p->val == q->val) 18 { 19 bool templ = isSameTree(p->left,q->left); 20 bool tempr = isSameTree(p->right,q->right); 21 return templ&&tempr; 22 23 } 24 else 25 { 26 return false; 27 } 28 } 29 else if (p!=NULL) 30 { 31 return false; 32 } 33 else if (q!=NULL) 34 { 35 return false; 36 } 37 else 38 { 39 return true; 40 } 41 42 } 43 };
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { // Start typing your C/C++ solution below // DO NOT write int main() function if ((p!= NULL) && (q!=NULL)) { if (p->val == q->val) { bool templ = isSameTree(p->left,q->left); bool tempr = isSameTree(p->right,q->right); return templ&&tempr; } else { return false; } } else if (p!=NULL) { return false; } else if (q!=NULL) { return false; } else { return true; } } };