/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
static int max;
public int maxDepth(TreeNode root) {
max = 0;
check(root, 0);
return max;
}
public void check(TreeNode node, int cnt) {
if (node == null) {
if (cnt > max) {
max = cnt;
}
return;
}
check(node.left, cnt + 1);
check(node.right, cnt + 1);
}
}
릿코드에서 static을 쓸때는 꼭 시작점에서 한번씩 초기화해줘야함.
'코딩테스트' 카테고리의 다른 글
[leetcode] 1926. Nearest Exit from Entrance in Maze (0) | 2024.05.26 |
---|---|
[leetcode] 841. Keys and Rooms (0) | 2024.05.26 |
[leetcode] 1732. Find the Highest Altitude (0) | 2024.05.18 |
[leetcode] 643. Maximum Average Subarray I (0) | 2024.05.18 |
[leetcode] 392. Is Subsequence (0) | 2024.05.18 |