2013年9月12日

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. 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; next = null; } 7 * } 8 */ 9 /**10 ... 阅读全文

posted @ 2013-09-12 14:46 Step-BY-Step 阅读(460) 评论(0) 推荐(0) 编辑

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 ofeverynode never differ by more than 1.the tree is only balanced if:The left and right subtrees' heights differ by at most o 阅读全文

posted @ 2013-09-12 14:28 Step-BY-Step 阅读(172) 评论(0) 推荐(0) 编辑

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.solution:采用BFS的方式遍历tree即可。 同时为了知道是在哪一层 加入一个每一层结束的signal node。 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int ... 阅读全文

posted @ 2013-09-12 13:47 Step-BY-Step 阅读(169) 评论(0) 推荐(0) 编辑

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 andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文

posted @ 2013-09-12 13:02 Step-BY-Step 阅读(135) 评论(0) 推荐(0) 编辑

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 \ 4 \ 5 \ 6Hints:If you notice carefully ... 阅读全文

posted @ 2013-09-12 12:48 Step-BY-Step 阅读(216) 评论(0) 推荐(0) 编辑

Distinct Subsequences

摘要: Given a stringSand a stringT, count the number of distinct subsequences ofTinS.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,"ACE" 阅读全文

posted @ 2013-09-12 11:57 Step-BY-Step 阅读(199) 评论(0) 推荐(0) 编辑

导航