dfs-104. Maximum Depth of Binary Tree

来源:互联网 发布:快手上的淘宝优惠群 编辑:程序博客网 时间:2024/06/09 18:03

题目:


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) {        if (!root)return 0;        int Left = 1;        int Right = 1;        if (root->left)            Left = Left + maxDepth(root->left);        if (root->right)            Right = Right + maxDepth(root->right);        return  (Left > Right)? Left:right;    }};


0 0
原创粉丝点击