dfs-104. Maximum Depth of Binary Tree

題目:


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;
    }
};


發佈了37 篇原創文章 · 獲贊 0 · 訪問量 7025
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章