LeetCode107 Binary Tree Level Order Traversal II

来源:互联网 发布:python交易策略 编辑:程序博客网 时间:2024/05/21 16:56

详细见:leetcode.com/problems/binary-tree-level-order-traversal-ii


Java Solution: github

package leetcode;import java.util.Iterator;/* * 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   7return its bottom-up level order traversal as:[  [15,7],  [9,20],  [3]] */import java.util.LinkedList;import java.util.List;import java.util.Queue;import tools.TreeNode辅助.TreeNode;public class P107_BinaryTreeLevelOrderTraversalII {static int N = Integer.MIN_VALUE;public static void main(String[] args) {TreeNode root = null;List<List<Integer>> ans = null;root = tools.TreeNode辅助.A_生成满二叉树(new int[] {1,2, 3,4, 5, 6, 7});root = tools.TreeNode辅助.A_生成满二叉树(new int[] {3,9, 20,N, N, 15, 7});root = tools.TreeNode辅助.A_生成满二叉树(new int[] {3});ans = new Solution().levelOrderBottom(root);System.out.println(ans.size());Iterator<List<Integer>> it = ans.iterator();while (it.hasNext()) {tools.Utils.B_打印List_Integer_OneLine(it.next());}}/* * AC * 3 ms */static class Solution {List<List<Integer>> ans = new LinkedList<>();    public List<List<Integer>> levelOrderBottom(TreeNode root) {        if (root == null) {        return ans;        }    Queue<TreeNode> q1 = new LinkedList<TreeNode>();    Queue<Integer> q2 = new LinkedList<Integer>();    q1.add(root);    q2.add(1);    int pre_int = -1;    List<Integer> list_root = null;        while (! q1.isEmpty()) {        TreeNode root_now = q1.poll();        int root_int = q2.poll();        if (root_int != pre_int) {        if (pre_int != - 1) {        ans.add(0, list_root);        }        pre_int = root_int;        list_root = new LinkedList<>();        }        list_root.add(root_now.val);        if (root_now.left != null) {        q1.add(root_now.left);        q2.add(root_int + 1);        }        if (root_now.right != null) {        q1.add(root_now.right);        q2.add(root_int + 1);        }        }        if (pre_int != -1 && list_root != null) {        ans.add(0, list_root);        }    return ans;    }}}


C Solution: github

/*    url: leetcode.com/problems/binary-tree-level-order-traversal-ii    AC 3ms 28.85%*/#include <stdio.h>#include <stdlib.h>struct TreeNode {    int val;    struct TreeNode *left;    struct TreeNode *right;};typedef struct TreeNode * ptn;typedef struct TreeNode stn;typedef int* T;typedef struct al sal;typedef struct al * pal;struct al {    int capacity;    int size;    T* arr;};pal al_init(int capacity) {    pal l = (pal) malloc(sizeof(sal));    if (capacity < 1) return NULL;    l->arr = (T*) malloc(sizeof(T) * capacity);    l->capacity = capacity;    l->size = 0;    return l;}void al_expand_capacity(pal l) {    T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));    int i = 0;    for (i = 0; i < l->capacity; i ++)        new_arr[i] = l->arr[i];    free(l->arr);    l->arr = new_arr;    l->capacity = l->capacity * 2 + 1;}void al_add_last(pal l, T v) {    if (l->capacity == l->size) al_expand_capacity(l);    l->arr[l->size] = v;    l->size ++;}void al_free_all(pal l) {    int i = 0;    for (i = 0; i < l->size; i ++)        free(l->arr[i]);    free(l->arr);    free(l);}T* al_convert_to_array_free_l(pal l) {    T* arr = l->arr;    free(l);    return arr;}typedef ptn V;typedef struct sl ssl;typedef struct sl * psl;struct sl {    int capacity;    int size;    V* arr;};psl sl_init(int capacity) {    psl l = (psl) malloc(sizeof(ssl));    if (capacity < 1) return NULL;    l->arr = (V*) malloc(sizeof(V) * capacity);    l->capacity = capacity;    l->size = 0;    return l;}void sl_expand_capacity(psl l) {    V* new_arr = (V*) malloc(sizeof(V) * (l->capacity * 2 + 1));    int i = 0;    for (i = 0; i < l->capacity; i ++)        new_arr[i] = l->arr[i];    free(l->arr);    l->arr = new_arr;    l->capacity = l->capacity * 2 + 1;}void sl_add_last(psl l, V v) {    if (l->capacity == l->size) sl_expand_capacity(l);    l->arr[l->size] = v;    l->size ++;}void sl_free_all(psl l) {    free(l->arr);    free(l);}void swap_psl(psl* l1, psl* l2) {    psl l = *l1;    *l1 = *l2;    *l2 = l;}void swap_int_star(int** a, int** b) {    int* c = *a;    *a = *b;    *b = c;}void reverse(pal l) {    int i = 0, j = l->size - 1;    while (i < j) {        swap_int_star(&(l->arr[i]), &(l->arr[j]));        i ++;        j --;    }}int** levelOrderBottom(ptn n, int** cn, int* rn) {    psl l1 , l2 ;    int i = 0, i2 = 0, *t = NULL;     pal l = NULL, ln = NULL;    if (n == NULL) return NULL;    l1 = sl_init(16);    l2 = sl_init(16);    l = al_init(16);    ln = al_init(16);    sl_add_last(l1, n);    while (l1->size != 0) {        t = (int*) malloc(sizeof(int) * l1->size);        for (i = 0; i < l1->size; i ++)            t[i] = l1->arr[i]->val;        al_add_last(l, t);        t = (int*) malloc(sizeof(int) * 1);        t[0] = l1->size;        al_add_last(ln, t);        l2->size = 0;        for (i = 0; i < l1->size; i ++) {            if (l1->arr[i]->left != NULL)                sl_add_last(l2, l1->arr[i]->left);            if (l1->arr[i]->right != NULL)                sl_add_last(l2, l1->arr[i]->right);        }        swap_psl(&l1, &l2);    }    sl_free_all(l1);    sl_free_all(l2);    *cn = (int*) malloc(sizeof(int) * ln->size);    for (i = 0; i < ln->size; i ++)        (*cn)[ln->size - 1- i] = ln->arr[i][0];    al_free_all(ln);    *rn = l->size;    reverse(l);    return al_convert_to_array_free_l(l);}


Python Solution: github

#coding=utf-8'''    url: leetcode.com/problems/binary-tree-level-order-traversal-ii    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年4月28日    @details:    Solution: 56ms 59.43%'''class TreeNode(object):    def __init__(self, x):        self.val = x        self.left = None        self.right = Noneclass Solution(object):    def levelOrderBottom(self, n):        """        :type n: TreeNode        :rtype: List[List[int]]        """        if n == None: return []        ans, l = [], [n]        while len(l) != 0:            ans.append([nn.val for nn in l])            ll = []            for nn in l:                if nn.left != None: ll.append(nn.left)                if nn.right != None: ll.append(nn.right)            l = ll        ans.reverse()        return ans


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝刚满月不喜欢在床上睡怎么办 别人给我打了收货款不发货怎么办 业务员私收货款公司不发货怎么办 付款后商家没发货也不退钱怎么办 苹果7P修过主板耗电严重怎么办 oppo手机进水了开不了机怎么办 手机弯了变形但不影响使用怎么办 拼多多留错电话怎么办如果已经发货 苹果6手机后壳变形了怎么办 京东退货保修卡丢了怎么办 如果京东买的显示器屏碎了怎么办 在微信上买东西退货不退钱怎么办 手机分期付款不还款被起诉了怎么办 朋友用我的花呗分期不还钱怎么办 朋友用我身份证办分期不还钱怎么办 我手机号被别人绑定信用卡了怎么办 5s用不了4g网络怎么办 红米手机返回键没了怎么办 小米手机进水了开不了机怎么办 手机返回键和菜单键失灵怎么办 苹果5s指纹按键坏了怎么办 小米手机安卓系统耗电量大怎么办? 苹果5s充不进去电怎么办 苹果手机6s返回键失灵怎么办 本人被骗同时被利用骗了别人怎么办 京东取消订单后货到了该怎么办 京东电信日租卡流量顶置了怎么办 苹果6s进水后闪光灯不亮怎么办 华为手机情景义停车事项过期怎么办 拼多多付款后商品下架了怎么办 淘宝上买化妆品买到假货了怎么办 找苹果官网解id发票丢了怎么办 客人已交订金但要取消宴席怎么办 京东买的小米电视碎屏了怎么办 京东购买的电视碎屏了怎么办 淘宝上买手机不能用不给退怎么办 天猫申请退货退款卖家不处理怎么办 在淘宝买到货到付款的假苹果怎么办 跟朋友买手机买到假货怎么办 在淘宝网上买到不合格的产品怎么办 淘宝打假师打了我的店铺怎么办