binaryTree sum notlimitted head and tail
感想
详细代码
感想
应用了全局变量
所有函数的返回值现在没有用上。
代码之双递归
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param sum int整型
* @return int整型
*/
typedef struct TreeNode pTreeNode;
int gSum=0;
int gRet=0;
void dfs(pTreeNode * pTreeNode, int currSum){
//exit
if(NULL == pTreeNode){
return ;
}
//exit 2
if(pTreeNode->val == currSum){
gRet++;
}
//deep
dfs(pTreeNode->left , currSum - pTreeNode->val);
dfs(pTreeNode->right , currSum - pTreeNode->val);
}
int FindPath(struct TreeNode* root, int sum ) {
// write code here
if(NULL == root ){
return 0;
}
gSum = sum;
//base level get result
int result = 0;
//gRet
dfs(root,gSum);//from selft
//left
FindPath(root->left,gSum);
//right
FindPath(root->right,gSum);
//return final result
return gRet;
}