Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
解题思路:采用中序排列的方法递归地决定每个结点的数值;
#include
#include
#include
using namespace std;
//Definition...
分类:
其他好文 时间:
2015-01-30 16:00:24
阅读次数:
131
原题地址中序遍历二叉树,递归展开。别忘了将left设为NULL,如果忘掉的话可能报Runtime Error,而且这个RE很难查出原因。代码: 1 TreeNode *solve(TreeNode *root) { 2 if (!root) return NULL; 3 4 ...
分类:
其他好文 时间:
2015-01-30 14:31:21
阅读次数:
121
原题地址二叉树基本操作[ ]O[ ][ ][ ]O代码: 1 TreeNode *restore(vector &inorder, vector &postorder, int ip, int pp, int len) { 2 if (len == 0) 3 ...
分类:
其他好文 时间:
2015-01-30 10:36:55
阅读次数:
186
原题地址基本二叉树操作。O[ ][ ][ ]O[ ]代码: 1 TreeNode *restore(vector &preorder, vector &inorder, int pp, int ip, int len) { 2 if (len left = r...
分类:
其他好文 时间:
2015-01-30 10:36:12
阅读次数:
125
原题地址对于已排序数组,二分法递归构造BST代码: 1 TreeNode *buildBST(vector &num, int i, int j) { 2 if (i > j) 3 return NULL; 4 5 int m = (i + j) /2; 6 T...
分类:
其他好文 时间:
2015-01-30 10:26:25
阅读次数:
158
原题地址跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了。代码: 1 TreeNode *buildBST(ListNode *head, int len) { 2 if (len next; 9 ...
分类:
其他好文 时间:
2015-01-30 10:24:52
阅读次数:
192
Given a binary tree, findits maximum depth.
The maximum depth is thenumber of nodes along the longest path from the root node down to the farthestleaf node.
可用递归,效率低。
这里用类似层次遍历的算法。设置一个队列和两个int变量...
分类:
其他好文 时间:
2015-01-30 09:10:56
阅读次数:
261
题目链接:https://oj.leetcode.com/problems/same-tree/
题目:
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally i...
分类:
其他好文 时间:
2015-01-30 09:07:15
阅读次数:
177
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ 2 2
/ \ / 3 4 4 3
But the f...
分类:
编程语言 时间:
2015-01-30 09:04:26
阅读次数:
242
这篇文章的主题是动态规划, 主要介绍LeetCode中一维动态规划的题目, 列表如下:Climbing StairsDecode WaysUnique Binary Search TreesMaximum SubarrayMaximum Product SubarrayBest Time to Bu...
分类:
其他好文 时间:
2015-01-30 06:35:32
阅读次数:
195