题意:给定一棵二叉树,返回按层遍历的结果
思路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
题意:中序遍历
思路:采用递归实现。因为函数声明是返回一个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
第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解。看来还得好好研究下递归算法。题目:求一棵树的最大深度。思路:递归地求取左子树最大深度
和 右子树最大深度,返回较大值即为 整棵树的 最大深度。代码:public int maxDepth(TreeNode root) { ...
分类:
其他好文 时间:
2014-05-14 23:07:14
阅读次数:
373
Binary Tree Preorder Traversal
Total Accepted: 18022 Total
Submissions: 51784My Submissions
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given b...
分类:
其他好文 时间:
2014-05-14 20:43:22
阅读次数:
242
点击打开链接
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 975 Accepted Submission(s): 289
Problem Description
...
分类:
其他好文 时间:
2014-05-14 20:18:47
阅读次数:
366