Chinaunix首页 | 论坛 | 博客
  • 博客访问: 117138
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-24 16:22
文章存档

2014年(53)

我的朋友

发布时间:2014-12-06 18:21:44

需要牢记BST的定义:左边的所有节点都小于根节点,根节点小于右边的所有节点所以这样的做法是错误的:bool isValidBST(TreeNode *root) {    if(root==NULL)        return true;    bool .........【阅读全文】

阅读(1361) | 评论(0) | 转发(0)

发布时间:2014-12-06 17:47:20

Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?.........【阅读全文】

阅读(1107) | 评论(0) | 转发(0)

发布时间:2014-12-06 17:21:01

Given a binary tree, flatten it to a linked list in-place.这题直观的做法就是先序遍历,一个节点一个节点的塞到list里面,但是为了避免毁掉原本的链接,所以在递归中使用临时变量存储节点TreeNode * p=NULL;void f(TreeNode * root){    if(root==NULL).........【阅读全文】

阅读(929) | 评论(0) | 转发(0)

发布时间:2014-12-06 16:46:38

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.比较奇葩的设定,要是没有左子树,那么就要算右子树的最小高度。int minDepth(TreeNode *root) {   &n.........【阅读全文】

阅读(857) | 评论(0) | 转发(0)

发布时间:2014-12-06 15:42:18

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 identical and the nodes have the same value.很像那个镜像的树,思想完全一样。bool isSameTree(TreeNode *p, TreeNode *q) {  .........【阅读全文】

阅读(800) | 评论(0) | 转发(0)
给主人留下些什么吧!~~
留言热议
请登录后留言。

登录 注册