- 博客(39)
- 收藏
- 关注
原创 Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路:题意给定一个n阶的楼梯,每次走一步或者两步,问到达顶部
2016-06-21 14:30:46 241
原创 House Robber II
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a ci
2016-06-17 14:33:21 197
原创 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house
2016-06-17 10:58:15 165
原创 Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree:1/ \2 3\5All root-to-leaf paths are:["1->2->5", "1->3"]思路:题目要求是打印出来一个二叉树的从根到叶子节点
2016-06-17 10:16:41 163
原创 Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
2016-06-16 16:48:27 221
原创 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 definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee
2016-06-16 15:20:36 197
原创 Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.思路:给定一个二叉搜索树,要求程序返回的是整个BST的第k小的节点值对于一个BST来说,其中序遍历的结果是升序的。那么在中序遍历访问节点的顺序上当第k个访问的时候,实际上这个节点就是题目要求的节点值
2016-06-16 14:46:56 179
原创 Invert Binary Tree
Invert a binary tree.4/ \2 7/ \ / \1 3 6 9to4/ \7 2/ \ / \9 6 3 1思路:题目要求将一个二叉树的左右孩子进行交换。递归解决即可/** * Definition for a binary tree node. * struct TreeNode { * int va
2016-06-16 14:34:45 194
原创 Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as poss
2016-06-16 14:11:57 180
原创 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 /
2016-06-16 10:58:22 245
原创 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()
2016-06-14 16:30:37 175
原创 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3},1\2/3return [3,2,1].思路:求一棵二叉树的后续遍历的序列方法一:递归/** * Definition fo
2016-06-14 15:40:21 180
原创 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3},1\2/3return [1,2,3].思路:题目要求一个二叉树的先序遍历结果方法一:递归方式/** * Definition
2016-06-14 15:24:06 248
原创 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
2016-06-14 15:04:46 150
原创 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
2016-06-14 14:52:50 174
原创 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
2016-06-07 16:08:13 146
原创 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 n
2016-06-07 15:40:14 150
原创 Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given1/ \2 5/ \ \3 4 6The flattened tree should look like:1\2\3\4\5\6思路:题目要求将一个二叉树修改为一
2016-06-06 15:06:11 172
原创 Path Sum II
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/ \4 8/ / \11 13 4/ \ / \
2016-06-06 11:21:27 178
原创 Path Sum
Given 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 below binary tree and su
2016-06-06 10:54:08 171
原创 Minimum Depth of Binary Tree
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.思路:给定一个二叉树,返回这个二叉树的最小深度。最小深度的定义是从roo
2016-06-04 16:34:40 156
原创 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 never diffe
2016-06-04 16:19:20 166
原创 Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:题目要求根据提供的有序的array,将其转化为一个平衡的二叉搜索树。可以考虑如下解决方式:(1)找到数组中的中间元素,以中间元素为分割点,作为树的根,前半部分为左子树的节点值,右半部分为右
2016-06-04 16:04:11 151
原创 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,#,#,15,7},
2016-06-04 15:55:53 148
原创 Maximum Depth of Binary Tree
Given 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 node.思路:返回一个二叉树的最大的高度。思考递归的解决方式:(1)如果
2016-06-04 11:31:23 187
原创 Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary
2016-06-03 15:48:53 133
原创 Binary Tree Level Order Traversal
Given 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 {3,9,20,#,#,15,7},3/ \9 20/ \15 7re
2016-06-03 15:28:16 120
原创 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric:1/ \2 2/ \ / \3 4 4 3But the following is not
2016-06-03 15:06:53 145
原创 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.思路:给定两个二叉树,判
2016-06-03 14:41:09 149
原创 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.
2016-06-03 10:22:51 185
原创 Unique Binary Search Trees II
Given 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 3 2 1\
2016-06-02 16:02:10 144
原创 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\ / / / \ \3 2 1 1 3 2/
2016-06-02 14:13:55 238
原创 Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3},1\2/3return [1,3,2].思路:二叉树中序遍历。如果节点不为NULL的话,先访问这个节点的left节点,再访问当前节点,最后
2016-06-02 10:20:08 168
原创 Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in
2016-06-01 15:48:37 184
原创 Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value
2016-06-01 15:03:56 156
原创 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?Subscribe to see which companies asked this question思路:判断一个链表含有的数值是不是一个回文的
2016-06-01 14:46:59 248
原创 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5思路:在一个链表中,移除等于给定数值的链表节点。使用
2016-06-01 14:27:22 131
原创 Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2↘c1 → c2 → c3↗ B: b1 → b2 → b3beg
2016-06-01 11:14:35 162
原创 Sort List
Sort a linked list in O(n log n) time using constant space complexity.思路:使用归并的方式来实现链表的排序。先将链表一直拆分到一个个元素,对于每一个元素都是有序的,然后两两进行合并成一个有序链表。解决代码如下:/** * Definition for singly-linked list. * struct Li
2016-06-01 10:44:32 154
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人