摘要: 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. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val... 阅读全文
posted @ 2013-04-14 13:23 冰点猎手 阅读(120) 评论(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. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right... 阅读全文
posted @ 2013-04-14 13:18 冰点猎手 阅读(126) 评论(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. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(... 阅读全文
posted @ 2013-04-14 12:09 冰点猎手 阅读(148) 评论(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... 阅读全文
posted @ 2013-04-14 10:35 冰点猎手 阅读(146) 评论(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 ofevery node never differ by more than 1.分析:检查每个节点的左右子节点的高度差/** * Definition for binary tree * struct TreeNode { * int val; *... 阅读全文
posted @ 2013-04-12 12:54 冰点猎手 阅读(198) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int maxProfit(vector<int> &prices) { // Start typing your C/C++ solution below // DO NOT write int main() function if(prices.size() <2) return 0; int *profit = new int[prices.size()]; memset(profit,0, sizeof(int)*prices.size()) ; i... 阅读全文
posted @ 2013-04-11 23:31 冰点猎手 阅读(136) 评论(0) 推荐(0) 编辑
摘要: class Solution {public: int maxProfit(vector<int> &prices) { // Start typing your C/C++ solution below // DO NOT write int main() function if( prices.size() < 2 ) return 0; int result = 0; int min = 0,i; for(i = 1; i< prices.size(); i++) { ... 阅读全文
posted @ 2013-04-10 23:18 冰点猎手 阅读(110) 评论(0) 推荐(0) 编辑
摘要: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */// 三种遍历的递归实现void preOrder(TreeNode * root){ if(NULL == root ) return ; visit(root->val); preOrder(root->left); pr... 阅读全文
posted @ 2013-04-10 20:12 冰点猎手 阅读(161) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 分析: 对于当前的股票价格收益最大是当前价格减去之前股票的最低价,所以需要一个指针来保存当前股票前面的最低价。 阅读全文
posted @ 2013-04-10 11:00 冰点猎手 阅读(139) 评论(0) 推荐(0) 编辑
摘要: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int getMax(TreeNode *root){ if(root == NULL) return 0; int left = getMax(roo... 阅读全文
posted @ 2013-04-10 10:20 冰点猎手 阅读(127) 评论(0) 推荐(0) 编辑