[LeetCode]104. Maximum Depth of Binary Tree

    xiaoxiao2021-03-25  64

    [LeetCode]104. Maximum Depth of Binary Tree

    题目描述

    思路

    深搜,节点为NULL返回0,其余比较左右子树大小,返回大值

    代码

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxDepth(TreeNode* root) { if (!root) return 0; int left = 1 + maxDepth(root->left); int right = 1 + maxDepth(root->right); return left > right ? left : right; } };
    转载请注明原文地址: https://ju.6miu.com/read-37633.html

    最新回复(0)