leetcode 104 Maximum Depth of Binary Tree

来源:互联网 发布:金蝶软件数据恢复 编辑:程序博客网 时间:2024/05/16 17: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.

Subscribe to see which companies asked this question


/** * 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 preOrder(TreeNode *root, int hight, int &maxH) {if(root!=NULL) {if(hight>maxH) maxH=hight;preOrder(root->left, hight+1, maxH);preOrder(root->right, hight+1, maxH);}return maxH;}    int maxDepth(TreeNode* root) {        int maxH = 0;        if(root==NULL) return 0;return preOrder(root, 1, maxH);    }};


0 0