leetcode[107]:Binary Tree Level Order Traversal II
2015-07-20 14:37
555 查看
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,#,#,15,7},
return its bottom-up level order traversal as:
在Binary Tree Level Order Traversal 基础上修改下标。
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,#,#,15,7},
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
/** * 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 *columnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */ int maxDepth(struct TreeNode* root) { int i, j; i=j=1; if(!root) return 0; if(root->left) i += maxDepth(root->left); if(root->right) j += maxDepth(root->right); if( i > j ) return i; return j; } int** levelOrderBottom(struct TreeNode* root, int** columnSizes, int* returnSize) { int res[1000][1000]; int size[1000]; struct TreeNode stack[10000]; int i,ress,j,k,l,ll; int **result; l=maxDepth(root); if(!root) return NULL; result= (int**)malloc(sizeof(int*)*l); *columnSizes = (int*)malloc(sizeof(int*)*l); *returnSize = l; i=j=k=0; stack[0] = *root; size[l-1]=1; ll=1; ress=1; while(ll!=0) { ll=0; printf("%d",l-i-1); result[l-i-1] = (int*)malloc(sizeof(int*)*size[l-i-1]); for(j=0;j<size[l-i-1];j++) { result[l-i-1][j]=stack[k+j].val; if(stack[k+j].left) {ll++;stack[ress++]=*stack[k+j].left; } if(stack[k+j].right) {ll++;stack[ress++]=*stack[k+j].right;} } k += j; (*columnSizes)[l-i-1]=size[l-i-1]; i++; size[l-i-1]=ll; } return result; }
在Binary Tree Level Order Traversal 基础上修改下标。
相关文章推荐
- 命令行快速技巧:如何定位一个文件
- cmd tree命令 以树形格式罗列文件
- 推荐8款jQuery轻量级树形Tree插件
- tree 以树形格式罗列文件
- EasyUI Tree+Asp.net实现权限树或目录树导航的简单实例
- E3 tree 1.6在Firefox下显示问题的修复方法
- js树形控件脚本代码
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- Spark Decision Tree
- 带check的treeView
- extjs 4 treepanel locked and expand
- JQuery EasyUI 实现tree的右键菜单
- flex tree 的两个小技巧
- Tree.log
- gnome文件管理器(nautilus)树形结构文件窗口 多窗口
- 排序binary tree
- eclipse C/C++ 问题(launch failed,binary not found)
- launch failed.Binary not found in Linux/Ubuntu解决方案
- python递归实现Easyui combotree树
- InnoDB--------查询IOT B+ Tree的高度