1. 程式人生 > >LeetCode 100. Maximum Depth of Binary Tree 二叉樹的最大深度

LeetCode 100. Maximum Depth of Binary Tree 二叉樹的最大深度

/**
 * 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==nullptr)
            return 0;
        return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
};

相關推薦

LeetCode 100. Maximum Depth of Binary Tree 深度

/**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     TreeNode *left;  *     TreeNode *right;  *     TreeN

LeetCode 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

LeetCode 104.Maximum Depth of Binary Tree (深度)

題目描述: 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 /

[LeetCode] 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

Maximum Depth of Binary Tree-深度

code clas etc dep 是否 兩種 depth In .com 求二叉樹的最大深度,是常見的一種二叉樹算法問題,主要解決辦法有兩種,一種是使用遞歸求解,另一種是非遞歸方式求解。這裏給出遞歸求解方法。遞歸方法無需判斷左右子樹是否為空。 問題來源於https://

LeetCode-111-Minimum Depth of Binary Tree(短路徑)

Q: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root no

[LeetCode] Maximum Width of Binary Tree 寬度

example imu idt count ted pla con integer out Given a binary tree, write a function to get the maximum width of the given tree. The wi

[LeetCode] Minimum Depth of Binary Tree 深度

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest lea

662. Maximum Width of Binary Tree寬度

hide play evel view define font defined nodes 如果 [抄題]: Given a binary tree, write a function to get the maximum width of the given tree.

111Minimum Depth of Binary Tree深度

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度  2

[CareerCup] 4.4 Create List at Each Depth of Binary Tree 的各層建立連結串列

4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll ha

leetCode 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 t

leetcodeMaximum 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 do

[LeetCode] 104. Maximum Depth of Binary Tree Java

font from max clas [] 高度 java ret 使用 題目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the

4/100. Maximum Depth of Binary Tree

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #

LeetCode--104. Maximum Depth of Binary Tree

題目連結:https://leetcode.com/problems/maximum-depth-of-binary-tree/ 求樹的最大深度,一言以蔽之:子節點樹的最大深度加1就是父節點樹的最大深度。遞迴題目想通了就好簡單!!! class Solution { public i

Leetcode 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long

JavaScript刷LeetCode -- 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

leetcode 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

LeetCodemaximum-depth-of-binary-tree

[程式設計題] maximum-depth-of-binary-tree 時間限制:1秒 空間限制:32768K   Given a binary tree, find its maximum depth. The maximum depth is the numb