일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 방통대
- 조지아텍
- Deep learning
- 프로그래머스
- 머신러닝
- 컴퓨터과학과
- LV1
- 매트랩
- 캐나다 영주권
- 온라인석사
- 코딩테스트
- C++
- 딥러닝
- leetcode
- 마니토바
- 알고리즘
- EOI
- machine learning
- 선형대수
- omscs
- 기본
- 방송통신대학교
- MATLAB
- mpnp
- 개발자
- zeros
- LAA
- 주정부이민
- cpp
- Plotting
Archives
- Today
- Total
Krononberg
1448. Count Good Nodes in Binary Tree.cpp 본문
이제 트리구조 포인팅이 어떻게 되는지 스스로 생각 가능해진다.
🔑 각 recursion의 max값을 저장하여, 현재 노드 val과 비교한다. 그리고 노드val >=max이면, res++.
주의. max값은 각 recursion에 속해야 한다. (global로 max값을 설정하면 안됨!)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int res =0;
void preorder(TreeNode*root,int max){
if(root==nullptr) return;
if(root->val >= max){
max = root->val;
res++;
}
preorder(root->left,max);
preorder(root->right,max);
return;
}
int goodNodes(TreeNode* root) {
preorder(root,INT_MIN);
return res;
}
};
'개발 로그 > 알고리즘' 카테고리의 다른 글
1380. Lucky Numbers in a Matrix.cpp (0) | 2021.10.21 |
---|---|
811. Subdomain Visit Count.cpp (0) | 2021.10.20 |
1742. Maximum Number of Balls in a Box.cpp (0) | 2021.10.18 |
2042. Check if Numbers Are Ascending in a Sentence.cpp (0) | 2021.10.18 |
1850. Minimum Adjacent Swaps to Reach the Kth Smallest Number.cpp (0) | 2021.10.17 |