algorithm
lazycece
http://lazycece.com
展开
-
【树】判断数A为数B的子结构
题目:输入两棵二叉树A,B,判断B是不是A的子结构,约定空树不是任意一个树的子结构。 思路:递归 算法: 如下 bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) { bool result=false; if(pRoot1==nullptr||pRoot2==nullptr) ...原创 2016-08-26 10:56:17 · 334 阅读 · 0 评论 -
【树】最小数目深度
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 run(TreeNode *root) { i...原创 2016-08-22 20:59:38 · 302 阅读 · 0 评论 -
【链表】单向链表的反转
三指针法遍历,一次遍历则完成反转: ListNode* ReverseList(ListNode* pHead) { //no node if(pHead==nullptr) return pHead; ListNode* pre=nullptr,*mid=pHead,*last=nullptr; //more nod...原创 2016-08-21 23:27:15 · 366 阅读 · 0 评论 -
【链表】合并两个排序的链表
First ------ 递归思路,需要重新创建一个链表,空间复杂度O(1) ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { if(pHead1==nullptr) return pHead2; if(pHead2==nullptr) return pHead1; Li...原创 2016-08-21 23:21:31 · 489 阅读 · 0 评论