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 leaf node.
查詢二叉樹的深度,遞迴去做,注意判斷邊界就好了。/** * Definition for binary tree * 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)return 0; return max(maxDepth(root->left),maxDepth(root->right))+1; } };
相關推薦
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 do
LeetCode:543. Diameter of Binary Tree(找出二叉樹中最大的半徑)
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest
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 d
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
leetcode--maximum-depth-of-binary-tree(給定一棵二叉樹,找出它的最大深度)
給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,1
LeetCode刷題-——Maximum Depth of Binary Tree(二叉樹的最大深度)
連結:點選開啟連結題目:給定一個二叉樹,找出其最大深度。二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。Example:給定二叉樹 [3,9,20,null,null,15,7], 3
[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
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
題目連結: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
LeetCode 104.Maximum Depth of Binary Tree (二叉樹的最大深度)
題目描述: 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 /
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
104. Maximum Depth of Binary Tree(1.private 2.void)
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 farthe
Leetcode111: Minimum Depth of Binary Tree(二叉樹最小深度)
先看Leetcode104:Maximum Depth of Binary Tree,求二叉樹最大深度。有兩種解法,基於DFS(深度優先搜尋)思想的方法使用遞迴計算:class Solution { public: int maxDepth(TreeNode *roo
LeetCode(42) 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
LeetCode 100. Maximum Depth of Binary Tree 二叉樹的最大深度
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeN
LeetCode 111. 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
[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree
input pre .com add rip like efi height find 97. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree 本題難度: Easy Topic: Binary
leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
treenode right left leetcode oot tco binary 判斷 root 104: class Solution { public: int maxDepth(TreeNode* root) { if(roo