[C]LeetCode:Maximum Depth of Binary Tree

来源:互联网 发布:intent传递int数据 编辑:程序博客网 时间:2024/06/06 06:59

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.

题目获取一个二叉树的最大深度:
首先想到的当然是DFS算法

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     struct TreeNode *left; *     struct TreeNode *right; * }; */int maxDepth(struct TreeNode* root) {    if (root==NULL)        return 0;    int suml=maxDepth(root->left);    int sumr=maxDepth(root->right);    return suml>sumr?suml+1:sumr+1;}
0 0
原创粉丝点击