Maximum Depth Of Binary Tree

Yohan Kim·2021년 9월 4일
0

problem

트리의 최대 깊이를 구하는 문제입니다.

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

solution

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

후기

BST를 전에 구현한 적 있어서 쉽게 풀 수 있었습니다.

profile
안녕하세요 반가워요!

0개의 댓글