摘要:
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 / / \ \ 2 ...
阅读全文
posted @ 2013-03-01 13:49
CoderJesse
阅读(131)
推荐(0)
摘要:
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.动规题。用A[i1][i2]表示s1中的前i1个元素和s2中前i2个元素能否构成s3的前i1+i3个元素。A[i1][i2] = (
阅读全文
posted @ 2013-03-01 13:48
CoderJesse
阅读(135)
推荐(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?利用中序周游从小到大的顺序检查,找到第一个异常元素。再利用中序周游从大到小的顺序检查,找到第一个异常元素。将这两个元素交换即可。 1 class Solution
阅读全文
posted @ 2013-03-01 13:37
CoderJesse
阅读(121)
推荐(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 keys less than the node's key.The right subtree of a node contains only nodes with keys greater than the node's key.Both the left an
阅读全文
posted @ 2013-03-01 13:34
CoderJesse
阅读(115)
推荐(0)
摘要:
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.不用解释吧。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * ...
阅读全文
posted @ 2013-03-01 13:33
CoderJesse
阅读(107)
推荐(0)
摘要:
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: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati...
阅读全文
posted @ 2013-03-01 13:32
CoderJesse
阅读(129)
推荐(0)
摘要:
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 tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3...
阅读全文
posted @ 2013-03-01 13:26
CoderJesse
阅读(137)
推荐(0)
摘要:
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 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 ...
阅读全文
posted @ 2013-03-01 13:21
CoderJesse
阅读(141)
推荐(0)
摘要:
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.首先,前序周游序列的最后一个元素是根节点。那么需要在中序周游序列中找到这个元素的下标,这样就可以将两个序列分别分割开,用同样的方法找根节点的左右子节点。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ...
阅读全文
posted @ 2013-03-01 13:20
CoderJesse
阅读(125)
推荐(0)
摘要:
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.首先,后序周游序列的最后一个元素是根节点。那么需要在中序周游序列中找到这个元素的下标,这样就可以将两个序列分别分割开,用同样的方法找根节点的左右子节点。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ...
阅读全文
posted @ 2013-03-01 13:17
CoderJesse
阅读(149)
推荐(0)