- 博客(58)
- 收藏
- 关注
原创 二叉树(二叉搜索树)上的两节点的公共祖先节点(235和236)
一、二叉搜索树上的两节点的公共祖先 235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the defini
2017-01-15 00:07:43 537
原创 98. Validate 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 node’s key. The right
2017-01-18 13:33:39 357
原创 95. Unique Binary Search Trees II--需要细看
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below.1 3
2017-01-18 13:02:34 268
转载 114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3
2017-01-17 20:08:41 192
转载 337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour,
2017-01-17 19:14:12 187
原创 450. Delete Node in a BST--非递归的写法可以思考
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.Basically, the deletion can be divided into t
2017-01-17 18:29:32 481
原创 257. Binary Tree Paths --非递归的代码还未写出
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree:1 / \ 2 3 \ 5 All root-to-leaf paths are:[“1->2->5”, “1->3”] Credits: Special thanks t
2017-01-15 22:35:05 449
原创 129. Sum Root to Leaf Numbers--注意非递归的方法
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total sum of al
2017-01-15 21:54:42 269
转载 95. Unique Binary Search Trees II--还未做
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below.1 3
2017-01-15 20:17:58 301
转载 96. Unique Binary Search Trees --继续研究
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example, Given n = 3, there are a total of 5 unique BST’s.1 3 3 2 1 \ /
2017-01-15 20:05:00 233
原创 117. Populating Next Right Pointers in Each Node II
Follow up for problem “Populating Next Right Pointers in Each Node”.What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.
2017-01-14 13:13:04 205
原创 116. Populating Next Right Pointers in Each Node
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,
2017-01-14 12:41:25 234
原创 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Follow up: What if the BST is mod
2017-01-14 12:15:46 201
原创 199. Binary Tree Right Side View(递归代码想不明白)
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example: Given the following binary tree, 1
2017-01-13 14:25:00 416
原创 构建二叉树(前中序构建105,中后序构建106)--非递归的方法值得研究
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.
2017-01-13 10:52:29 338
原创 108. Convert Sorted Array to Binary Search Tree(和把一个单链表转换成BST树的思想一致)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Subscribe to see which companies asked this question 解题思路:利用递归思想去解决,首先找到有序数组的中位数节点,即位于最中间位置的节点作为根节点,然后依
2017-01-13 09:54:42 382
转载 173. Binary Search Tree Iterator---这是一个类的设计题值得好好推敲
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and hasN
2017-01-12 21:58:21 326
原创 100. Same Tree和判断一个树是不是对称的思想一样
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.Subscribe to see wh
2017-01-12 16:49:09 315
原创 404. Sum of Left Leaves
Sum of Left Leaves Find the sum of all left leaves in a given binary tree Example:3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return
2017-01-12 16:08:13 331
原创 101. Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric:1 / \ 2 2 / \ / \ 3 4 4
2017-01-12 13:50:04 254
转载 4. Median of Two Sorted Arrays
4. Median of Two Sorted ArraysThere are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be
2017-01-11 19:53:02 635
原创 34. Search for a Range
34. Search for a RangeGiven an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the
2017-01-11 17:23:31 314
转载 汉诺塔问题
在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘,求把圆盘从下面开始按大小顺序重新摆放在另一根柱子上需要移动多少次。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。解答:我们可以拿n=3的时候举例子:易知f(1)=1,f(2)=3,f(3)=7……;当n=2时候(1,2,3分别表示小中大三块盘子):当n=3时候:
2017-01-11 16:53:48 1859
原创 35. Search Insert Position
35. Search Insert PositionGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may
2017-01-11 15:58:58 267
原创 二叉树的几种遍历的方法(后序遍历的另外一种递归实现还需要思考)
1、先序遍历:94. Binary Tree Inorder TraversalGiven a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3
2017-01-10 16:57:05 526
原创 226. Invert Binary Tree
226. Invert Binary TreeInvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by
2017-01-10 14:42:29 279
转载 437. Path Sum III
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it
2017-01-09 22:43:37 331
原创 113. Path Sum II
113. Path Sum IIGiven 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,
2017-01-09 22:21:20 297
原创 (非递归的方法待研究)112. Path Sum
112. Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the bel
2017-01-09 22:08:47 595
原创 (待思考--不采用逆置等的思想)107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal IIGiven 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).Fo
2017-01-09 18:03:58 285
原创 102. Binary Tree Level Order Traversal
102. Binary Tree Level Order TraversalGiven a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree
2017-01-09 17:28:40 298
转载 (待研究)111. Minimum Depth of Binary Tree
111. Minimum Depth of Binary TreeGiven 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
2017-01-09 16:13:09 297
原创 110. Balanced Binary Tree
110. Balanced Binary TreeGiven 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 s
2017-01-09 15:08:33 273
原创 104. Maximum Depth of Binary Tree
104. Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf no
2017-01-09 14:31:33 342
转载 222. Count Complete Tree Nodes
222. Count Complete Tree NodesGiven a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except po
2017-01-07 19:35:03 264
转载 (待研究)50. Pow(x, n)
50. Pow(x, n)Implement pow(x, n).leetcode 上的top解法nest myPowdouble myPow(double x, int n) { if(n0) return 1/x * myPow(1/x, -(n+1)); if(n==0) return 1; if(n==2) return
2017-01-07 19:00:22 279
转载 (待进一步研究吃透)209. Minimum Size Subarray Sum
209. Minimum Size Subarray SumGiven an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0
2017-01-07 18:46:17 290
原创 162. Find Peak Element
162. Find Peak ElementA peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array m
2017-01-07 17:34:22 320
原创 441. Arranging Coins
441. Arranging CoinsYou have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircas
2017-01-07 12:24:55 282
原创 (待整理)300. Longest Increasing Subsequence
300. Longest Increasing SubsequenceGiven an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest inc
2017-01-07 12:01:11 307
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人