摘要: 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.---/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right... 阅读全文
posted @ 2013-09-21 12:35 LEDYC 阅读(118) 评论(0) 推荐(0)
摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?confused what"{1,#,2,3}"means?> read more on how binary tree is seria 阅读全文
posted @ 2013-09-21 12:24 LEDYC 阅读(101) 评论(0) 推荐(0)
摘要: 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 keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri 阅读全文
posted @ 2013-09-21 12:19 LEDYC 阅读(144) 评论(0) 推荐(0)
摘要: Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return false.---DP---Solution1 : 2维数组public class Solution { public boolean isInterleave(String s 阅读全文
posted @ 2013-09-21 11:59 LEDYC 阅读(142) 评论(0) 推荐(0)
摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2013-09-21 11:34 LEDYC 阅读(194) 评论(0) 推荐(0)
摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文
posted @ 2013-09-21 11:29 LEDYC 阅读(135) 评论(0) 推荐(0)
摘要: Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?confused what"{1,#,2,3}"means?> read more on how binary tree is serialized on OJ.---ino 阅读全文
posted @ 2013-09-21 11:17 LEDYC 阅读(138) 评论(0) 推荐(0)
摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ?m?n? length of list.---/** * Definition for singly-linked list. * public c 阅读全文
posted @ 2013-09-21 01:18 LEDYC 阅读(132) 评论(0) 推荐(0)
摘要: Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]---public class... 阅读全文
posted @ 2013-09-21 01:06 LEDYC 阅读(167) 评论(0) 推荐(0)