发布时间:2014-12-06 18:21:44
需要牢记BST的定义:左边的所有节点都小于根节点,根节点小于右边的所有节点所以这样的做法是错误的:bool isValidBST(TreeNode *root) { if(root==NULL) return true; bool .........【阅读全文】
发布时间: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?.........【阅读全文】
发布时间: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).........【阅读全文】
发布时间: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.........【阅读全文】
发布时间: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) {  .........【阅读全文】