摘要: 平衡二叉树的判断,DFS,一直没想到怎么同时返回结点的长度和状态,把高度放到参数里面比较简便! 1 class Solution { 2 public: 3 bool isBalanced(TreeNode *root) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int height = 0; 7 return getHeight(root, height); 8 9 ... 阅读全文
posted @ 2013-09-01 23:34 Exio 阅读(188) 评论(0) 推荐(0) 编辑
摘要: 数据结构的基础知识,根据中序、后序遍历或者先序、中序遍历构建二叉树;典型的递归;中序、后序的代码: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */10 class Solution {11 public:12 TreeNode ... 阅读全文
posted @ 2013-09-01 18:30 Exio 阅读(196) 评论(0) 推荐(0) 编辑
摘要: 有点类似于层级遍历,用两个数组保存上下两层的结点即可!同样的代码两道题目都可以过! 1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:1... 阅读全文
posted @ 2013-09-01 13:11 Exio 阅读(130) 评论(0) 推荐(0) 编辑
摘要: 典型的dp,最开始写了个递归的,过不了大数据, 1 class Solution { 2 public: 3 int numDistinct(string S, string T) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int m = S.length(); 7 int n = T.length(); 8 return getDistinct(S, T, m - 1, n -... 阅读全文
posted @ 2013-09-01 10:17 Exio 阅读(119) 评论(0) 推荐(0) 编辑