LeetCode OJ Maximum Depth of Binary Tree

来源:互联网 发布:理解信息与数据 编辑:程序博客网 时间:2024/06/13 01:15

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

class Solution {public:    int maxDepth(TreeNode *root) {        max = 0;        now = 0;        if (root != NULL) gogogo(root);        return max;    }    void gogogo(TreeNode *nowRoot) {        now++;        if (nowRoot->left != NULL) gogogo(nowRoot->left);        if (nowRoot->right != NULL) gogogo(nowRoot->right);        if (nowRoot->left == NULL && nowRoot->right == NULL) max = max > now ? max : now;        now--;    }    int now;    int max;};


0 0
原创粉丝点击