摘要: 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-07-02 01:00 一只会思考的猪 阅读(136) 评论(0) 推荐(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 3 Note: Bonus points if you could solve it both recursively and itera... 阅读全文
posted @ 2013-07-02 00:54 一只会思考的猪 阅读(138) 评论(0) 推荐(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], [20,9], [ 阅读全文
posted @ 2013-07-02 00:38 一只会思考的猪 阅读(144) 评论(0) 推荐(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], [20,9], [ 阅读全文
posted @ 2013-07-02 00:35 一只会思考的猪 阅读(143) 评论(0) 推荐(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.class Solution {public: int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() f... 阅读全文
posted @ 2013-07-02 00:29 一只会思考的猪 阅读(116) 评论(0) 推荐(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.class Solution {public: void f(vector &preorder,int s1, int e1,vector & inorder,int s2, int e2, TreeNode *& root){ if (s1 > e1){ return; ... 阅读全文
posted @ 2013-07-02 00:25 一只会思考的猪 阅读(143) 评论(0) 推荐(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.class Solution {public: void f(vector &inorder,int s1, int e1,vector & postorder,int s2, int e2, TreeNode *& root){ if (s1 > e1){ return; ... 阅读全文
posted @ 2013-07-02 00:18 一只会思考的猪 阅读(150) 评论(0) 推荐(0) 编辑
摘要: 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],]class Solution 阅读全文
posted @ 2013-07-01 23:55 一只会思考的猪 阅读(164) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.class Solution {public: void f(vector & v, int start, int end, TreeNode *&root){ if (start > end){ return; } int m = start + (end - start)/2; if (!root){ ... 阅读全文
posted @ 2013-07-01 23:24 一只会思考的猪 阅读(109) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.class Solution {public: void f(vector & v, int start, int end, TreeNode *&root){ if (start > end){ return; } int m = start + (end - start)/2; if (!ro... 阅读全文
posted @ 2013-07-01 23:19 一只会思考的猪 阅读(138) 评论(0) 推荐(0) 编辑