Sobes.tech
Junior — Senior

Checking the identity of two binary trees

livecode

Task condition

It is necessary to implement the isSameTree function, which determines whether two binary trees are the same. Two trees are considered the same if their structures match and all corresponding nodes contain the same values.

class ForInterview {
public:
    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) {}
    };

    bool isSameTree(TreeNode* p, TreeNode* q) {
        // implement
    }
};