摘要: 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 ofeverynode never differ by more than 1.这道题目总是理解错,最开始理解就是左子树的任何一个叶子节点和右子树的任何一个叶子节点的depth差不能大于1. 并且左右子树都是平衡的,所以设了leftMinDep, leftM 阅读全文
posted @ 2012-10-27 22:40 chkkch 阅读(4457) 评论(5) 推荐(0) 编辑
摘要: Given a binary tree, return thebottom-up level ordertraversal 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}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]BFS,... 阅读全文
posted @ 2012-10-27 22:15 chkkch 阅读(994) 评论(3) 推荐(0) 编辑
摘要: Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7},DFS遍历方法: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right;... 阅读全文
posted @ 2012-10-27 22:04 chkkch 阅读(4901) 评论(2) 推荐(0) 编辑
摘要: Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3},非递归版本:用一个栈来模拟递归的情况,首先思考inorder traverse的递归函数traverse(left tree);visit current nodetraverse(right tree);也就是说我们把一个节点压入栈中,首先它会先递归访问左子树(左节点入栈),再访问本身(这个时候这个节点就可以出栈了),在访问右子树(右节点入栈)。最后我们定义一个数据结构1 阅读全文
posted @ 2012-10-27 21:48 chkkch 阅读(3326) 评论(0) 推荐(0) 编辑
摘要: Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.这题最开始的思路是用map<string, vector<sting> >来保存与string对应的anagrams,然后有一张vector<string> word来保存key string,但问题是每次新的string要遍历word来进行比较是否存在,用的是一个count[26]来保存字符的个数,然后一一比较。最后发现其实先把每个s 阅读全文
posted @ 2012-10-27 20:53 chkkch 阅读(2633) 评论(0) 推荐(0) 编辑
摘要: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8模拟加法,注意最后加法结束时可能有进位 阅读全文
posted @ 2012-10-27 19:32 chkkch 阅读(1469) 评论(0) 推荐(0) 编辑
摘要: Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".这题用数组来做可能更简单,但考虑到可能面试的时候要求不能开额外的数组,就只能对string操作了。最主要的是把进位这部分写对。 1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 // Start typing your C/C++ solu 阅读全文
posted @ 2012-10-27 19:23 chkkch 阅读(4169) 评论(0) 推荐(1) 编辑
摘要: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)The solution set must not contain duplicate triplets. For e... 阅读全文
posted @ 2012-10-27 19:12 chkkch 阅读(1108) 评论(0) 推荐(0) 编辑
摘要: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)The solution set must not contai 阅读全文
posted @ 2012-10-27 18:21 chkkch 阅读(3471) 评论(0) 推荐(0) 编辑
摘要: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.For example, given array S = {-1 2 1 -4}, and target = 1.The sum that is closest to th 阅读全文
posted @ 2012-10-27 18:03 chkkch 阅读(5705) 评论(1) 推荐(0) 编辑