/** * 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) { int v = ((p == NULL) << 1) + (q == NULL); if (v == 0x2 || v == 0x1) return false; // one NULL the other not NULL if (v == 0x3) return true; // both NULL if (p->val != q->val) return false; // value not match return isSameTree(p->left, q->left) // check subtree && isSameTree(p->right, q->right); } };
继续水
LeetCode SameTree
时间: 2024-10-07 22:02:53