中级农民
- 积分
- 106
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-8-18
- 最后登录
- 1970-1-1
|
【9/22 Sat】Tree-base DFS
Binary Tree Problems:
考点本质:DFS
• 第一类:求值,求路径类二叉树的问题
• 第二类:结构变化类二叉树问题
• 第三类:BST 问题
【第一类:求值,求路径类二叉树的问题】
Tree的题目离不了的是recursion下面的三道题目就是一种类型,利用recursive helper function分别recursion出left subtree和right subtree 的结果,再合并不断update result
124 binary tree maximum path sum the path must contain at least one node
687 longest univalue path find the length of the longest path where each node in the path has the same value
543 diameter of binary tree compute the longest length of the diameter of the tree
分析:需要求整棵树上最大/最长的path或者sum, 一定是需要走完整棵树才知道结果,那么需要一个recursive的helper function来不断向下面走。同时需要一个global variable来不断update记录所遇到过的最大的答案
做法:在主函数中callhelper function即可,在helper fucntion中
• 首先要有return case which you need to return 0 at this moment.
• 然后用int left = helper(root.left); int right = helper(root.right) 来分别记录当前left subtree 和right subtree 的答案 (devide 步骤)
○ 有时在做选择的时候,如max path sum需要subtree sum为正数,相加才最大,那么
○ int left = Math.max(0,helper(root.left)) 表示subtree可选可不选
• 再update global variable 如:longest = Math.max(longest, left + right + root.val)是情况而定
• 最后return helper function的值,记住是半边tree的subresult
○ 如 return Math.max(left, right) + root.val;
Subtree with maximum average(lintcode597)是124题目的拓展
这道题有点棘手,因为在不断dfs的recursion中既要知道当前subtree最大的sum又要知道subtree节点的个数。解决的方法就是:新建一个ResultType class,其中包括sum和count这两个preperty。那么dfs每次返回的是resultType的obect instead of just int variable。这个时候就需要有两个global variable,一个是最后返回的subtree node和当前最优秀的resultType object。在dfs中比较当前subtree的sum average并即使update。
**另外一个小策略:当想比较 a/b > c/d时不妨表示成: a * d > c * b 避免了整出后的取整问题
226. Invert Binary Tree
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
4
/ \
7 2
/ \ / \
9 6 3 1
public void invertBinaryTree(TreeNode root) {
if (root == null) { return; }
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertBinaryTree(root.left);
invertBinaryTree(root.right);
}
124 binary tree maximum path sum && minimum subtree 题目看似相似但其实不同
124: 只要找到 maximum的path就好,所以每次面临的是两个选择:左tree or 右tree,所以每次dfs返回的值也是左tree或右tree较大的那个 + root.val
Minimum subtree
每次就是为了得到subtree的值:left + right + root.val
257 Binary Tree Paths: 返回所有root to leaf 的paths
DFS 每次查看左节点和右节点是否同时为null,不是的话,就分别用dfscall下一个左节点 or 右节点
【第二类:二叉树结构/形态发生变化】
114. Flatten Binary Tree to Linked List
【第三类:BST】
• 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 and right subtrees must also be binary search trees.
【题目】
230. Kth Smallest Element in a BST
Follow up: 当BST经常被修改,怎么优化Kth smallest 这个操作?
在 TreeNode 中增加一个 counter,代表整个树的节点个数也可以用一个 HashMap<TreeNode, Integer> 来存储某个节点为代表的子树的节点个数在增删查改的过程中记录不断更新受影响节点的 counter
85. Insert Node in a Binary Search Tree
Given a binary search tree and a new tree node, insert the node into the tree. Return the node of the new BST.
public TreeNode insertNode(TreeNode root, TreeNode node) {
if (root == null) { return node; }
if (root.val > node.val) { root.left = insertNode(root.left, node); }
else { root.right = insertNode(root.right, node); } return root; }
补充内容 (2018-9-24 01:41):
将DFS分为三类复习:Tree-based dfs, combination based dfs, graph&permutation based dfs. 今天复习后两类。 |
|