Maximum Depth of Binary Tree

2016-09-18  本文已影响0人  一枚煎餅
Maximum Depth of Binary Tree.png

解題思路 :

從 root 開始做 recursive 回傳以左右兩邊 child 為起點的高度 取最大的一邊加上 1 (root本身) 如果 root 本身是 nullptr 就回傳 0

C++ code :

<pre><code>
/**

class Solution {

public:

/**
 * @param root: The root of binary tree.
 * @return: An integer
 */
int maxDepth(TreeNode *root) {
    // write your code here
    if(root == nullptr) return 0;
    else return 1 + max(maxDepth(root->left), maxDepth(root->right));
}

};
<code><pre>

上一篇 下一篇

猜你喜欢

热点阅读