您的位置:首页 > 其它

leetcode 104求二叉树的最大深度

2017-11-11 14:08 399 查看
/**

 * 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==NULL)

        {

            return 0;

        }

        int leftdepth=maxDepth(root->left);

        int rightdepth=maxDepth(root->right);

        return (leftdepth>rightdepth?leftdepth+1:rightdepth+1);

        

    }
};

思路:

通过递归遍历

首先确定大概思想,根节点不为空就是比较左右子树大小,取大的加一,然后传入左右孩子节点地址进行递归。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐