Leetcode--easy系列6

#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.

求二叉樹的深度。二叉樹的操作許多都可以用遞歸

//7ms
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int maxDepth(struct TreeNode* root) {
    int l_depth,r_depth;
    if(!root)
        return 0;
    else
    {
        l_depth = maxDepth(root->left);
        r_depth = maxDepth(root->right);
        return (l_depth >= r_depth) ? (l_depth+1):(r_depth+1);
    }        
}

#110 Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

判斷給定二叉樹是否是平衡二叉樹。。。深度差小於等於1.可以遞歸求每個結點的深度,判斷其子樹高度差。

//8ms
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int maxDepth(struct TreeNode* root) {
    int l_depth,r_depth;
    if(!root)
        return 0;
    else
    {
        l_depth = maxDepth(root->left);
        r_depth = maxDepth(root->right);
        return (l_depth >= r_depth) ? (l_depth+1):(r_depth+1);
    }        
}

bool isBalanced(struct TreeNode* root) {
    int k;
    if(!root)//空樹
        return true;
        
    k=maxDepth(root->left)-maxDepth(root->right);    
    if(abs(k)>1)
        return false;
    else
        return isBalanced(root->left) && isBalanced(root->right);
}

#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 to the nearest leaf node.

求二叉樹的最小深度---
//4ms
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int minDepth(struct TreeNode* root) {
    int l_depth,r_depth;
    if(!root)
        return 0;
    if(!root->left)
        return 1+minDepth(root->right);
    if(!root->right)
        return 1+minDepth(root->left);
    if(root->right && root->left)
    {
        l_depth = minDepth(root->left);
        r_depth = minDepth(root->right);
        return (l_depth < r_depth) ? (l_depth+1) : (r_depth+1);
    }    
}
#112 Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

判定指定二叉樹是否存在路徑和等於指定值。

//8ms
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool hasPathSum(struct TreeNode* root, int sum) {
    if(!root)
        return false;
    if( root->left==NULL && root->right==NULL)
        return sum == root->val;
    else
        return hasPathSum(root->left,sum - root->val) || hasPathSum(root->right,sum - root->val); 
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章