https://leetcode.com/problems/maximum-depth-of-binary-tree/
求一個二元樹最大深度並回傳。
設置兩個變數計算左節點長度與右節點長度,利用遞迴特性尋訪將其對應變數加一,最後比較大小回傳。
int maxDepth(struct TreeNode* root) {
int l_num=0,r_num=0;
if(root == NULL)
return 0;
if(root -> right == NULL && root -> left == NULL)
return 1;
if(root -> left != NULL)
{
l_num = 1+maxDepth(root -> left);
}
if(root -> right != NULL)
{
r_num = 1+maxDepth(root -> right);
}
return l_num > r_num ? (l_num) : (r_num);
}
var maxDepth = function(root) {
var l_num=0,r_num=0;
if(root == null)
return 0;
if(root.right == null && root.left == null)
return 1;
if(root.left != null)
{
l_num = 1+maxDepth(root.left);
}
if(root.right != null)
{
r_num = 1+maxDepth(root.right);
}
return l_num > r_num ? (l_num) : (r_num);
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
It’s not difficult to make a decision. It’s hard to put it into action and stick to it.
做一個決定,並不難,難的是付諸行動,並且堅持到底