上一页 1 2 3 4 5 6 7 8 9 10 ··· 16 下一页
摘要: 题目来源: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意分析: 给出一颗二叉树的中序遍历和后续遍历,还原这个树。 题目思路: 这题和上一题类似,用递归的思想,先 阅读全文
posted @ 2016-03-09 15:02 Ry_Chen 阅读(453) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: 根据二叉树的中序和前序遍历结果,反推这个树,假设只有一个这样的树。 题目思路: 前序遍历的第一个树 阅读全文
posted @ 2016-03-07 18:44 Ry_Chen 阅读(657) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/maximum-depth-of-binary-tree/ 题意分析: 返回一个树的最大层数。 题目思路: 这里用递归的思想,maxlevel = max( maxleft, maxright) + 1. 代码(python): 阅读全文
posted @ 2016-03-07 18:31 Ry_Chen 阅读(595) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意分析: Z字宽度遍历树。 题目思路: 这题可以用比较取巧的方法。首先获得宽度遍历的结果,然后将第二层的翻转就可以了。 代码(python): 阅读全文
posted @ 2016-03-07 18:26 Ry_Chen 阅读(533) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/binary-tree-level-order-traversal/ 题意分析: 宽度优先搜索一颗二叉树,其中同一层的放到同一个list里面。比如: 3 / \ 9 20 / \ 15 7返回 [ [3], [9,20], [1 阅读全文
posted @ 2016-03-07 18:08 Ry_Chen 阅读(553) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/symmetric-tree/ 题意分析: 判断一棵树是不是镜像树。 题目思路: 判断左子树的左右子树是不是和右子树的右左子树相同。 代码(python): # Definition for a binary tree node 阅读全文
posted @ 2016-03-07 17:54 Ry_Chen 阅读(530) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/same-tree/ 题意分析: 判断两棵树是否相等。 题目思路: 用递归的思想,先判断根节点,再判断左右子树。 代码(python): # Definition for a binary tree node. # class 阅读全文
posted @ 2016-03-07 17:46 Ry_Chen 阅读(344) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树。 题目思路: 如果是没有空间复杂度限制还是比较简单。首先将树的值取出来,然后排序,将相应的位置判断是否正确。如果要特定 阅读全文
posted @ 2016-03-07 17:39 Ry_Chen 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/validate-binary-search-tree/ 题意分析: 给定一个二叉树,判断这个二叉树是否二叉搜索树,也就是严格按照左子树小于等于节点和右子树。 题目思路: 树的题目,我们首先想到的就是递归的思想。这里也可以用递归 阅读全文
posted @ 2016-03-07 17:21 Ry_Chen 阅读(328) 评论(0) 推荐(0) 编辑
摘要: 题目来源: https://leetcode.com/problems/interleaving-string/ 题意分析: 给定字符串s1,s2,s3,判断s3是否由s1和s2穿插组成。如“abc”由“ac”,“b”组成,而“cba”不是。 题目思路: 这是一个动态规划问题。令ans[i][j]为 阅读全文
posted @ 2016-02-24 21:58 Ry_Chen 阅读(206) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 10 ··· 16 下一页