Post

leetcode(리트코드)104-Maximum Depth of Binary Tree

leetcode 104 - Maximum Depth of Binary Tree 문제입니다.

1. 문제

https://leetcode.com/problems/maximum-depth-of-binary-tree/


2. Input , Output

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • -100 <= Node.val <= 100

3. 분류 및 난이도

Eazy 난이도 문제입니다.
leetcode Top 100 Liked 문제입니다.


4. 문제 해석

  • 트리의 깊이를 찾아 리턴합니다.

5. code

python

1
2
3
4
5
6
7
8
9
10
11
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root == None : 
            return 0
        return max(self.maxDepth(root.left),self.maxDepth(root.right))+1

c++

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
/**
 * 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 cal(TreeNode* root)
    {
        if(root!=nullptr)
        {
            return max(cal(root->left),cal(root->right))+1;
        }
        return 0;
    }
    
    int maxDepth(TreeNode* root) {
        return cal(root);
    }
};

6. 결과 및 후기, 개선점

코드에 대한 설명이 필요하신 분은 댓글을 달아주세요.!!

c++ 100% python 98%

This post is licensed under CC BY 4.0 by the author.