文章目录
二叉树的路径和
112. 路径总和
先序遍历。
public boolean hasPathSum(TreeNode root, int targetSum) {
int curSum=0;
return backTracking(root,curSum,targetSum);
}
public boolean backTracking(TreeNode root,int cur,int targetSum){
if(root==null){
return false;
}
cur = cur+root.val;
if(root.left==null && root.right==null && cur==targetSum){
return true;
}
return backTracking(root.left,cur,targetSum) || backTracking(root.right,cur,targetSum);
}
class Solution {
List<List<Integer>> result;
LinkedList<Integer> path;
public List<List<Integer>> pathSum (TreeNode root,int targetSum) {
result = new LinkedList<>();
path = new LinkedList<>();
travesal(root, targetSum);
return result;
}
private void travesal(TreeNode root, int count) {
if (root == null) return;
path.offer(root.val);
count -= root.val;
if (root.left == null && root.right == null && count == 0) {
result.add(new LinkedList<>(path));
}
travesal(root.left, count);
travesal(root.right, count);
path.removeLast(); // 回溯
}
}
构造二叉树
106.从中序与后序遍历序列构造二叉树
class Solution {
HashMap<Integer,Integer> inorderArrayMap = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
for(int i = 0;i < inorder.length; i++) {
inorderArrayMap.put(inorder[i], i);//妙啊!将节点值及索引全部记录在哈希表中
}
return buildTree(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
}
private TreeNode buildTree(int[] inorder,int inorderStart,int inorderEnd,int[] postorder,int postorderStart,int postorderEnd) {
if(inorderStart>inorderEnd || postorderStart>postorderEnd){
return null;
}
TreeNode root = new TreeNode(postorder[postorderEnd]);
int index = inorderArrayMap.get(root.val);//获取对应的索引
// 位于[inorderStart,index-1] 属于左子树,位于[index+1,inorderEnd]属于右子树
// 位于[postorderStart,postorderStart+leftLengt]属于左子树
root.left = buildTree(inorder,inorderStart,index-1,postorder,postorderStart,postorderStart+index-inorderStart-1);
root.right = buildTree(inorder,index+1,inorderEnd,postorder,postorderStart+index-inorderStart,postorderEnd-1);
return root;
}
}
105.从前序与中序遍历序列构造二叉树
class Solution {
HashMap<Integer, Integer> inorderArrayMap = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
for (int i = 0; i < inorder.length; i++) {
inorderArrayMap.put(inorder[i], i);//妙啊!将节点值及索引全部记录在哈希表中
}
return buildTree(inorder, 0, inorder.length - 1, preorder, 0, preorder.length - 1);
}
private TreeNode buildTree(int[] inorder, int inorderStart, int inorderEnd, int[] preorder, int preorderStart, int preorderEnd) {
if (inorderStart > inorderEnd || preorderStart > preorderEnd) {
return null;
}
TreeNode root = new TreeNode(preorder[preorderStart]);
int index = inorderArrayMap.get(root.val);//获取对应的索引
// 位于[inorderStart,index-1] 属于左子树,位于[index+1,inorderEnd]属于右子树
// 位于[postorderStart,postorderStart+leftLengt]属于左子树
root.left = buildTree(inorder, inorderStart, index - 1, preorder, preorderStart + 1, preorderStart + index - inorderStart);
root.right = buildTree(inorder, index + 1, inorderEnd, preorder, preorderStart + index - inorderStart + 1, preorderEnd);
return root;
}
}
654. 最大二叉树
public class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return construct(nums, 0, nums.length);
}
public TreeNode construct(int[] nums, int l, int r) {
if (l == r)
return null;
int max_i = max(nums, l, r);
TreeNode root = new TreeNode(nums[max_i]);
root.left = construct(nums, l, max_i);
root.right = construct(nums, max_i + 1, r);
return root;
}
public int max(int[] nums, int l, int r) {
int max_i = l;
for (int i = l; i < r; i++) {
if (nums[max_i] < nums[i])
max_i = i;
}
return max_i;
}
}
合并二叉树
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) return root2;
if (root2 == null) return root1;
TreeNode newRoot = new TreeNode(root1.val + root2.val);
newRoot.left = mergeTrees(root1.left,root2.left);
newRoot.right = mergeTrees(root1.right,root2.right);
return newRoot;
}
}
二叉搜索树
常用思路:
- 中序遍历后二叉搜索树是递增的序列。
- 前后指针。
二叉搜索树的遍历
二叉搜索树的遍历是有方向的,不需要回溯。
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while (root != NULL) {
if (root->val > val) root = root->left;
else if (root->val < val) root = root->right;
else return root;
}
return NULL;
}
};
98.验证二叉搜索树
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
思路
二叉搜索树的中序遍历是有序的
TreeNode pre = null;
public boolean isValidBST2(TreeNode root) {
if (root == null) {
return true;
}
if (isValidBST2(root.left)) {
return false;
}
if (pre != null && pre.val > root.val) {
return false;
}
pre = root;
if (isValidBST2(root.right)) {
return false;
}
return true;
}
二叉树的公共祖先
思路
首先理解二叉树的公共祖先的定义。
- 出现一个节点root,左节点left包含p,右节点right包含q,或者右节点包含p,左节点包含q,那么root就是公共祖先
- root等于q 或者 root等于p,另外一个q或p出现在节点的左子树或者右子树中
要找到最近公共祖先,从低向上进行回溯遍历,找到第一个祖先就是最近公共祖先。因此需要使用后续遍历的方法,先遍历完叶子节点,再处理当前节点的逻辑
考虑通过递归对二叉树进行后序遍历,当遇到节点 pp 或 qq 时返回。从底至顶回溯,当节点 p, qp,q 在节点 rootroot 的异侧时,节点 rootroot 即为最近公共祖先,则向上返回 rootroot 。
代码
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == q || root == p) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) { // 最近公共祖先
return root;
} else if (left != null) { // 如果right==null,说明右子树没有p,q,返回左子树
return left;
} else if (right != null) {
return right;
} else {
return null;
}
}
思路二
- 定义一个函数contains(),对于给定的根节点root,是否包含q或者p
- 后续遍历,先计算出root的左右子树中是否包含q或p,记为left,right
- 对于当前节点,判断是否是公共祖先。
3.1 如果 left && right,说明root是公共祖先
3.2 如果root.val = p或者q,另外一个节点在root的左子树或者右子树中,说明root是公共祖先。
TreeNode ans;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
contains(root, p, q);
return ans;
}
/**
* 树是否包含p或者q
* @param root
* @param p
* @param q
* @return
*/
private boolean contains(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return false;
}
boolean cur = root.val == p.val || root.val == q.val;
boolean left = contains(root.left, p, q);
boolean right = contains(root.right, p, q);
// 根据叶子节点的结果进行判断,后序遍历
if ((left && right) || (cur && left) || (cur && right)) {
ans = root;
}
return cur || left || right;
}
二叉搜索树的公共祖先
根据二叉搜索树的特性,二叉搜索树的遍历是有方向的,公共祖先的值必定在两个值之间。
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (root.val > p.val && root.val > q.val) { // 不确定p跟q哪个更大
return lowestCommonAncestor(root.left, p, q);
} else if (root.val < q.val && root.val < p.val) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}
}
插入二叉搜索树
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
if (root.val < val) {
root.right = insertIntoBST(root.right, val);
} else {
root.left = insertIntoBST(root.left, val);
}
return root;
}
}