码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-02-26 13:09:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:dfs   result   getc   code   bsp   root   pre   rgb   nbsp   

 

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

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;
    }
};

 

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

标签:dfs   result   getc   code   bsp   root   pre   rgb   nbsp   

原文地址:https://www.cnblogs.com/siyu1915/p/14448491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!