검 지 Offer - (24) 이 진 트 리 와 특정한 값 의 경로
이 진 트 리 와 정 수 를 입력 하고 이 진 트 리 의 노드 값 과 정 수 를 입력 하기 위 한 모든 경 로 를 출력 합 니 다.경 로 는 나무의 뿌리 결점 에서 부터 잎 결점 이 지나 가 는 결점 까지 하나의 경 로 를 형성 하 는 것 으로 정의 된다.
다음 과 같이 구현:
//
// root
// , vector , vetor push_back pop
// :
//1. , 。 pop
//2. pop
class Solution
{
public:
vector<int> valueVec;//
vector<vector<int> > allPathVec;//
int currentNumber = 0;//
void Path(TreeNode *root, int expectNumber, int ¤tNumber)
{
if (root == NULL) return;//
valueVec.push_back(root->val);//
currentNumber += root->val;//
if (currentNumber == expectNumber && root->left == NULL && root->right == NULL)//
{
allPathVec.push_back(valueVec);//
return;//
}
else// left right
{
Path(root->left, expectNumber, currentNumber);
Path(root->right, expectNumber, currentNumber);
currentNumber -= valueVec.back();// pop
valueVec.pop_back();//
}
if (root->left != NULL || root->right != NULL)//
{
// ,
currentNumber -= valueVec.back();
valueVec.pop_back();
}
}
vector<vector<int> > FindPath(TreeNode* root, int expectNumber)
{
if (root == NULL) return allPathVec;//
Path(root, expectNumber, currentNumber);//
return allPathVec;
}
};
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
물체 검출의 평가 지표 IoU의 계산 방법Yolo나 SSD 등 물체 검출에서 평가 지표로 사용되는 IoU에 대해 조사했으므로 정리했습니다. IoU (Intersection over Union)는 두 영역이 얼마나 겹치는지를 나타내는 지표입니다. 두 영역의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.