算法提高
文章平均质量分 72
macidoo
成长中的程序媛
展开
-
【LeetCode笔记】判断一棵树是否为镜像
最初思路:1.求树的中序遍历,存入vector;2.判断vector是否为为镜像;/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : va原创 2017-04-08 15:15:47 · 934 阅读 · 0 评论 -
【LeetCode笔记】114.Flatten Binary Tree to Linked List(有疑惑)
题目:Total Accepted: 119447Total Submissions: 348844Difficulty: MediumContributor: LeetCodeGiven a binary tree, flatten it to a linked list in-place.For example,Given 1原创 2017-04-15 17:56:49 · 183 阅读 · 0 评论 -
【LeetCode笔记】113. Path Sum II DFS搜索+记录节点
题目:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5原创 2017-04-15 16:26:27 · 325 阅读 · 0 评论 -
【LeetCode笔记】Balanced Binary Tree 高度平衡二叉树
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node ne原创 2017-04-15 12:47:07 · 489 阅读 · 0 评论 -
【LeetCode笔记】Convert Sorted Array to Binary Search Tree 通过有序数列建立二叉搜索树
题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:每一次从有序数列的中间开始提一个出来作为当前的根节点。然后其左边就是左子树,右边就是右子树。然后对左右子树分别递归~/** * Definition for a binary原创 2017-04-11 23:23:07 · 227 阅读 · 0 评论 -
【LeetCode笔记】Binary Tree Level Order Traversal II 二叉树按层遍历,反向输出
题目:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,nu原创 2017-04-11 22:33:58 · 299 阅读 · 0 评论 -
【LeetCode笔记】Construct Binary Tree from Inorder and Postorder Traversal 中序、后序遍历求二叉树
思路:1.和上一题很像啦~~~后序遍历的最后一个点可以把中序遍历分割成前后两部分,前面的部分就是分割点的左子树,后面的部分就是分割点的右子树。以及类推可递归。具体细节和分析在上一题中都有,就不展开说了~/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod原创 2017-04-11 22:26:56 · 163 阅读 · 0 评论 -
【LeetCode笔记】Construct Binary Tree from Preorder and Inorder Traversal 前序、中序还原二叉树
思路:1.这道题还是借鉴了其他大佬的思路,仔细分析一下,前序遍历的第一个值是根节点值(假设为val),那么在中序遍历中val之前的都是根节点的左子树,val值之后的都是右子树。同理递归可还原二叉树。2.容易出错的地方:a.递归的子函数最好传入前序和中序的引用,然后每次更新前序和中序遍历的起始点和终止点,这样比较不容易出错。b.起始点和终止点的计算需要细心~尤其是得到middle之后原创 2017-04-11 21:49:56 · 236 阅读 · 0 评论 -
【LeetCode笔记】Maximum Depth of Binary Tree 二叉树最大深度 递归&非递归
轻而易举的敲下了经典的递归程序:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL)原创 2017-04-09 16:04:45 · 597 阅读 · 0 评论 -
【LeetCode笔记】Binary Tree Zigzag Level Order Traversal 二叉树Z字形遍历
思路:其实和上一题(二叉树的按层遍历)很像,开始想的是用队列存入一层的节点,这时计算队列的size,然后想要从第size个开始反过来取。咦,这不是先进后出嘛,用stack呀!不对,每取出来一个节点,都要继续存入这个节点的左右子节点,那不是这一层还没取完下一层就压栈进来了。既然想要根据位置存取方便,那当然是用vector!第二个问题在于从左往右存和从右往左存是每层交叉的,所以还需要多一个判断(这原创 2017-04-09 15:37:59 · 350 阅读 · 0 评论 -
【LeetCode笔记】Binary Tree Level Order Traversal 二叉树分层存储
1.Binary Tree Level Order Traversal 二叉树分层存储2.错误信息:Line 68: expected '}' at end of input原创 2017-04-08 23:40:20 · 346 阅读 · 0 评论 -
【LeetCode笔记】116. Populating Next Right Pointers in Each Node
题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next rig原创 2017-04-15 18:28:18 · 190 阅读 · 0 评论