zoukankan      html  css  js  c++  java
  • leetcode 113. Path Sum II

    题目描述:

    //深搜  结束条件 是叶子节点 并且当前路径的加和为sum 加入result中  否则退出。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<int> > ret;
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            if(root == NULL)
                return ret;
            vector<int> tmp;
            record(root,tmp,0,sum);
            return ret;
            
        }
        
        void record(TreeNode* root, vector<int> tmp , int now, int target){
            now += root->val;
            tmp.push_back(root->val);
            if(root->left == NULL && root->right == NULL){
                if(now == target)
                    ret.push_back(tmp);
                return;
            }
            if(root->left != NULL)
                record(root->left,tmp,now,target);
            if(root->right != NULL)
                record(root->right,tmp,now,target);
    
        }
    };
  • 相关阅读:
    [恢]hdu 2002
    [恢]hdu 2001
    [恢]hdu 2003
    [恢]hdu 1000
    ActionButton的处理流程 天龙packet
    ogre visibleflag
    Scaleform Gfx的Demo
    cegui 自定义控件
    PixWin
    在Ogre中加载自己的资源包
  • 原文地址:https://www.cnblogs.com/strongYaYa/p/6772665.html
Copyright © 2011-2022 走看看