题意:给定一棵二叉树,返回按层遍历的结果
思路1:bfs,定义一个新的struct,记录指针向节点的指针和每个节点所在的层
复杂度1:时间O(n),空间O(n)
思路2:dfs
递归函数:
void levelOrder(TreeNode *root, int level, vector<vector >&result)
表示把根为root的树按层存放在result中,其中level表示当前的层数
复杂度2:时间O(n),空间O(n)
相关题目:...
分类:
其他好文 时间:
2014-05-15 14:46:06
阅读次数:
355
Problem Description:
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys l...
分类:
其他好文 时间:
2014-05-15 11:22:21
阅读次数:
317
题意:从底往上按层遍历二叉树
思路:
思路和Binary Tree Level Order Traveral 一样,
即从上往下按层遍历二叉树,将每一层的节点存放到该层对应的数组中
最后将得到的数组倒转一下就可以了
按层遍历二叉树可用bfs,也可用dfs,但都要记录节点所在的层
复杂度:时间O(n), 空间O(n)...
分类:
其他好文 时间:
2014-05-15 06:32:14
阅读次数:
278
题意:中序遍历
思路:采用递归实现。因为函数声明是返回一个vector,所以每个子树返回的是该子树的中序遍历的结果
按照 左、根、右的次序把根和左右子树的vector合并起来就可以了...
分类:
其他好文 时间:
2014-05-15 06:21:09
阅读次数:
255
1、
??
Populating Next Right Pointers in Each Node
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate...
分类:
其他好文 时间:
2014-05-15 04:57:36
阅读次数:
221
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Binary Tree Preorder Traversal
Total Accepted: 17948 Total
Submissions: 51578
Given a binary tree, return the preorder tra...
分类:
其他好文 时间:
2014-05-15 04:37:26
阅读次数:
283
Same Tree
Total Accepted: 16072 Total
Submissions: 38790My Submissions
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal i...
分类:
其他好文 时间:
2014-05-15 04:00:39
阅读次数:
319
题目来源:UVa 10308 Roads in the North
题意:求距离最远的2点之间的距离
思路:裸的树的直径 或者树形DP
#include
#include
#include
using namespace std;
const int maxn = 100010;
struct node
{
int to, w;
node(){}
node(int to, int...
分类:
其他好文 时间:
2014-05-15 03:37:18
阅读次数:
295
这篇文章提出了backgroundjs这个新的概念,扩展了Node.js的能力,解决了Node在处理CPU密集任务时的短板。这个解决方案使得使用Node的开发人员只需要关注backgroundjs中的函数。比起多开进程或者新添加模块的解决方案更高效,通用和一致。...
分类:
Web程序 时间:
2014-05-14 21:16:18
阅读次数:
502
node.js API 英文原版
http://nodejs.org/api/all.html
node.js API 中文翻译
http://nodeapi.ucdok.com/#/api/
node.js API 中文翻译下载
http://download.csdn.net/detail/bad19876414641/4608699
javascrip...
分类:
Web程序 时间:
2014-05-14 15:15:10
阅读次数:
329