1379. 找出克隆二叉树中的相同节点

时间:2021-02-26 13:09:42   收藏:0   阅读:0

 

算是第一个独立完成的中等题了。对二叉树进行一次先序遍历即可。

class Solution {
public:
    TreeNode *targetNode;
    TreeNode *resultNode;

    TreeNode *getTargetCopy(TreeNode *original, TreeNode *cloned, TreeNode *target) {
        targetNode = target;
        bool res = dfs(cloned);
        return resultNode;
    }

    bool dfs(TreeNode *root) {
        if (root == NULL) return false;
        if (root->val == targetNode->val) {
            resultNode = root;
            return true;
        }
        if (root->left != NULL) dfs(root->left);
        if (root->right != NULL) dfs(root->right);
        return false;
    }
};

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!