LeetCode #9 (#104、#107、#108)

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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

   3
  / \
 9  20
   /  \
  15   7

return its depth = 3.

//Solution
//总结:求二叉树的高度,两边递归求最大即可
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int maxDepth(struct TreeNode* root){
    int zuo,you,max;
    if(root)
    {
        zuo = maxDepth(root->left);
        you = maxDepth(root->right);
        if(zuo>=you) max= zuo;
        else max = you;
        return (max+1);
    }
    else return 0;
}

107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

   3
  / \
 9  20
   /  \
  15   7

return its bottom-up level order traversal as:c

[
 [15,7],
 [9,20],
 [3]
]
//Solution
//总结:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

int getDepth(struct TreeNode* root){
    int zuo,you,max;
    if(root)
    {
        zuo = getDepth(root->left);
        you = getDepth(root->right);
        if(zuo>=you) max= zuo;
        else max = you;
        return (max+1);
    }
    else return 0;
}

void helper(struct TreeNode* root, int **ans, int maxDepth, int *returnColumnSizes, int depth)
{
    int index = 0;
    
    if(root==NULL)
        return;
    
    index = maxDepth - depth -1;
    
    ans[index] = (int*)realloc(ans[index], (returnColumnSizes[index]+1) * sizeof(int));
    ans[index][returnColumnSizes[index]] = root->val;
    returnColumnSizes[index]++;

    helper(root->left, ans, maxDepth, returnColumnSizes, depth+1);
    helper(root->right, ans, maxDepth, returnColumnSizes, depth+1);
    
}

int** levelOrderBottom(struct TreeNode* root, int* returnSize, int** returnColumnSizes){

    int depth=0;
    int **ans = NULL;
    depth = getDepth(root);
    
    ans = (int**)calloc(depth, sizeof(int*));
    (*returnColumnSizes) = (int*)calloc(depth, sizeof(int));
    
    helper(root, ans, depth, *returnColumnSizes, 0);

    *returnSize=depth;
    return ans;
}
108. Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

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.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

     0
    / \
  -3   9c
  /   /
-10  5
//Solution
//总结:以排序好的数组的中间的数作为root进行插入
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* convert(int* nums,int start,int end)
{
     if(start > end)
          return NULL;
     else{
         int mid = (start+end)/2;
         struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
         node->val = nums[mid];
         node->left = convert(nums,start,mid-1);
         node->right = convert(nums,mid+1,end);
         return node;
     }
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
    return convert(nums,0,numsSize-1);
}

发布了72 篇原创文章 · 获赞 10 · 访问量 5840

猜你喜欢

转载自blog.csdn.net/weixin_44198992/article/details/105402041