diff --git a/C++/100_Same_Tree.cpp b/C++/100_Same_Tree.cpp index f9c4e19f..62e2dd22 100644 --- a/C++/100_Same_Tree.cpp +++ b/C++/100_Same_Tree.cpp @@ -1,27 +1,6 @@ -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode() : val(0), left(nullptr), right(nullptr) {} - * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} - * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} - * }; - */ class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { - if(p == nullptr && q == nullptr) - return true; - if(p == nullptr || q == nullptr) - return false; - if(p->val != q->val) return false; - - return (isSameTree(p->right,q->right) && isSameTree(p->left, q->left)); + return (!p && !q) || (p && q && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right)); } -}; - -// Complexity Analysis: -// Time complexity : O(N), where N is a number of nodes in the tree, since one visits each node exactly once. -// Space complexity :O(log(N)) in the best case of completely balanced tree and O(N) in the worst case of completely unbalanced tree. \ No newline at end of file +}; \ No newline at end of file