
DFS
文章平均质量分 56
ljffdream
这个作者很懒,什么都没留下…
展开
-
【Leetcode】Convert Sorted List to Binary Search Tree
【题目】Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 【思路】这道题目就和array不一样,因为是linkedlist所以,不能随意访问到中间的元素,那么就只能一步一步从头开始顺序访问,从列表到BST,我们能感受到构原创 2014-11-26 12:59:59 · 314 阅读 · 0 评论 -
【LeetCode】Convert Sorted Array to Binary Search Tree
leetcode原创 2014-11-26 11:05:06 · 300 阅读 · 0 评论 -
[Leetcode]Number of Island
[题目] Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You ma转载 2015-07-07 09:55:24 · 289 阅读 · 0 评论 -
【Leetcode】Find the kth in the BST
[题目] [思路] BST的特点就是排序性,左边的节点一定比root小,右边的节点一定比root大。 另外就是二分性。 找第k个,可以先看左边树节点的个数,小于k的话,就保证了肯定是在右边。那么去找右边的k-left. k - left,就变成了新的k. 数root所在的树的节点数。这个就是用递归实现。想法就是,如果root自己本身就是null,那么就return 0; 否则至转载 2015-07-08 21:28:09 · 525 阅读 · 0 评论 -
【Leetcode】Path sum 2
【题目】 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转载 2015-07-27 22:44:56 · 325 阅读 · 0 评论 -
【Leetcode】Valid Binary Search Tree
【题目】 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the nod转载 2015-07-17 23:26:37 · 305 阅读 · 0 评论 -
【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal
【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. 【思路】 Hint: A good way to attempt this question is to work backwards. Approach this question by drawing a binary转载 2015-09-03 02:13:50 · 542 阅读 · 0 评论 -
【Leetcode】Construct binary tree from inorder and postorder traversal
【题目】 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 【思路】 pretty similar with that转载 2015-09-03 03:10:58 · 411 阅读 · 0 评论