CoderJesse  
wangjiexi@CS.PKU

2013年3月1日

摘要: 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}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]偷... 阅读全文
posted @ 2013-03-01 13:12 CoderJesse 阅读(110) 评论(0) 推荐(0)
 
摘要: 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 7return its level order traversal as:[ [3], [9,20], [15,7]]二叉树的广度优先周游,稍稍做一下处理。 1 class Solution { ... 阅读全文
posted @ 2013-03-01 13:09 CoderJesse 阅读(109) 评论(0) 推荐(0)
 
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这个问题比较容易想到O(nlogn)的方法,通过链表的计数来找到一个序列的中间节点。不过,存在一个更加高效的方法,可以优化到O(n)。 这个方法有点不容易想到。之前做过的用排好序的数组元素构造balanced BST,是通过下表找中间元素,从上到下建立树。通过链表找中间元素只能通过计数。但如果可以有一种方法可以只经历一次遍历链表,合理利用当前的计数值,就可以安排好各个节点分.. 阅读全文
posted @ 2013-03-01 13:07 CoderJesse 阅读(240) 评论(0) 推荐(0)
 
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.首先,对于一个序列,找到它的中间元素作为根节点。根节点的左子节点连接到用同样方法处理中间元素之前的序列得到的根节点,根节点的右子节点连接到用同样方法处理中间元素之后的序列得到的根节点。时间复杂度为O(n)。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *l... 阅读全文
posted @ 2013-03-01 12:53 CoderJesse 阅读(120) 评论(0) 推荐(0)
 
摘要: 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 differ by more than 1.思路很简单,对于每个节点,递归求得其左右子节点的高度。由此可以得到它是否是平衡二叉树。对于其左右子节点用同样的方法递归。当所有的节点都是平衡二叉树的时候,那么整棵树才是平衡二 阅读全文
posted @ 2013-03-01 12:46 CoderJesse 阅读(119) 评论(0) 推荐(0)
 
摘要: 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.周游二叉树即可,很简单。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right;... 阅读全文
posted @ 2013-03-01 12:41 CoderJesse 阅读(126) 评论(0) 推荐(0)
 
摘要: 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 / \ / \ 7 2 5 1return[ [5,4,11,2... 阅读全文
posted @ 2013-03-01 12:39 CoderJesse 阅读(160) 评论(0) 推荐(0)
 
摘要: 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 sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2013-03-01 12:35 CoderJesse 阅读(139) 评论(0) 推荐(0)

2013年2月28日

摘要: 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 \ 4 \ 5 \ 6递归实现,处理一个节点的时候,用同样的方法处理它的左右子节点... 阅读全文
posted @ 2013-02-28 12:52 CoderJesse 阅读(150) 评论(0) 推荐(0)
 
摘要: Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "A 阅读全文
posted @ 2013-02-28 12:38 CoderJesse 阅读(254) 评论(0) 推荐(0)