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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值