剑指offer - 面试题34: 二叉树中和为某一值的路径 - C++

二刷:

阿呀呀,其实看到题还是没思路,想了一会儿之后准备看之前的博客,但还是不甘心直接看代码,看了一下文字部分说path和result共用切之后要pop,于是还是自己去敲出来了,虽然挺波折的。由此可见,永远不要放弃思考,你可能比自己想象中要强,比之前做的还好。

结果如下

class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
        vector<int> path;
        vector<vector<int>> result;
        FindPathCore(root, expectNumber, path, result);
        return result;
    }
    void FindPathCore(TreeNode* pRoot, int expectNumber,
                      vector<int>& path, vector<vector<int>>& result) {
        if(pRoot == nullptr) return;
        path.push_back(pRoot->val);
        if(pRoot->left == nullptr && pRoot->right == nullptr) {
            if(pRoot->val == expectNumber){
                result.push_back(path);
            }
        } else {
            FindPathCore(pRoot->left, expectNumber-pRoot->val, path, result);
            FindPathCore(pRoot->right, expectNumber-pRoot->val, path, result);
        }
        path.pop_back();
    }
};

其实和之前的第二种差不多,只不过第二种为了简练把path和result放在成员变量,不用传来传去了,都可以。然而它还可以借鉴我这次写出的,删掉if句子,目前史上最简:

class Solution {
    vector<vector<int> > result;
    vector<int> path;
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if(root == nullptr) return result;
        path.push_back(root->val);
        bool isLeaf = root->left == nullptr && root->right == nullptr;
        if(isLeaf) {
            if(root->val == expectNumber) {
                result.push_back(path);
            }
        } else {
            FindPath(root->left, expectNumber - root->val);
            FindPath(root->right, expectNumber - root->val);
        }
        path.pop_back();
        return result;
    }
};

除了系统调用的最外层,FindPath的返回值根本没人接。只起到return作用,但是起到了检测nullptr情况的作用,也是挺巧妙的

另外值得一记的是我先写出的一个错误答案:

class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
        vector<int> path;
        vector<vector<int>> result;
        FindPathCore(root, expectNumber, path, result);
        return result;
    }
    void FindPathCore(TreeNode* pRoot, int expectNumber,
                      vector<int>& path, vector<vector<int>>& result) {
        if(pRoot == nullptr) {
            if(expectNumber == 0) {
                result.push_back(path);
            }
        } else {
            path.push_back(pRoot->val);
            FindPathCore(pRoot->left, expectNumber-pRoot->val, path, result);
            FindPathCore(pRoot->right, expectNumber-pRoot->val, path, result);
            path.pop_back();
        }
    }
};

是根据空节点来判断exectNumber到0没。然后发现答案错误,

错误case: [10, 5, 12, 4, 7], 22 

正确答案: [[10, 5, 7], [10, 12]]

我的答案: [[10, 5, 7], [10, 5, 7], [10, 12], [10, 12]]

我一看,错的不是很离谱,于是手动运行了一下代码,发现我的判断方式不仅麻烦(要判断好多次nullptr节点),而且根本就不能确定是不是叶节点。出现两倍的原因是叶节点的左右空孩子各push了一次。这还是刚好所有节点要么叶节点,要么左右孩子都有,要不然答案错的更离谱。

 

/*****************************************************/

看了剑指offer写出的答案:

class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> result;
        if(root == nullptr) return result;
        vector<int> path;
        int currentSum = 0;
        FindPathCore(root, expectNumber, path, currentSum, result);
        return result;
    }
     
    void FindPathCore(TreeNode* root, int expectNumber,
                 vector<int>& path, int currentSum,
                 vector<vector<int>>& result) {
         
        currentSum += root->val;
        path.push_back(root->val);
         
        bool isLeaf = root->left == nullptr && root->right == nullptr;
        if(isLeaf) {
            if (currentSum == expectNumber) {
                result.push_back(path);
            }
        } else {
            if(root->left != nullptr) {
                FindPathCore(root->left, expectNumber, path, currentSum, result);
            }
            if(root->right != nullptr) {
                FindPathCore(root->right, expectNumber, path, currentSum, result);
            }
        }
        path.pop_back();
    }
};

此题难点在于记录路径,解决方案:用一个vector<int> path 记录路径。难点在于处理完当前节点后,递归调用左右节点后,要把path中当前节点弹出。所有函数公用path。因为刚开始不懂,跟着代码走一遍之后明白了这步的意义。另外currentSum就不用去减了,因为传的是值而不是引用。

做完之后我不甘心地去讨论区逛了逛,发现一种更简练的写法:

class Solution {
    vector<vector<int> > result;
    vector<int> path;
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if(root == nullptr) return result;
        
        path.push_back(root->val);
        bool isLeaf = root->left == nullptr && root->right == nullptr;
        if(isLeaf) {
            if(root->val == expectNumber) {
                result.push_back(path);
            }
        } else {
            if(root->left != nullptr) {
                FindPath(root->left, expectNumber - root->val);
            }
            if(root->right != nullptr) {
                FindPath(root->right, expectNumber - root->val);
            }
        }
        path.pop_back();
        return result;
    }
};

将需要共用的变量作为类的变量,省去增加引用类型的参数。

expectNumber也是当前节点所需要的路径和,我刚开始看到这道题也是觉得应该边向下走边减去,这样更符合递归的思想。简化了参数,可以直接调用自身递归,不用再写一个函数。

只有直接调用的函数返回return有效,中间调用的函数的return不予理睬。

妙哉!

另外,牛客网题目上说要按数组长度先大后小输出,我不知道该咋做,索性提交后它也没检测这个~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值