树:
1. 对称二叉树
判断二叉树是否对称,其规律是左子树和右子树相当,把大问题,求整个二叉树是否对称,分解成小问题,左右的子二叉树是否对称。
递归法:
public static boolean isSymmetric(TreeNode treeNode){
if (treeNode == null){
return true;
}
return dfc(treeNode.left,treeNode.right);
}
private static boolean dfc(TreeNode left, TreeNode right){
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
if (left.val != right.val){
return false;
}
//子树的根节点相等时,则比较:左子树的左节点和右子树的右节点; 左子树的右节点和右子树的左节点。
return dfc(left.left,right.right) && dfc(left.right, right.left);
}
队列:
public boolean isSymmetric(TreeNode root) {
if(root == null || (root.left ==null && root.right ==null)) {
return true;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root.left);
queue.add(root.right);
while(!queue.isEmpty()) {
TreeNode leftPoll = queue.removeFirst();
TreeNode rightPoll = queue.removeFirst();
if(leftPoll == null && rightPoll == null) {
continue;
}
if(leftPoll == null || rightPoll == null) {
return false;
}
if(leftPoll.val != rightPoll.val) {
return false;
}
queue.add(leftPoll.left);
queue.add(rightPoll.right);
queue.add(leftPoll.right);
queue.add(rightPoll.left);
}
return true;
}
2. 路径总和
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ,判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。
叶子节点 是指没有子节点的节点。
这个题目非常简单。 可采用递归或者双队列方法。
递归法: 求路径和,那么通过遍历树,记录节点的值,如果叶子节点的累加值等于sum,则找到了目标路径。取巧的方法是 sum-当前节点值 = 子节点值 那么就代表存在目标路径。
public static boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
if (root.left == null && root.right == null) {
return root.val == sum;
}
return hasPathSum(root.left, sum - root.val) && hasPathSum(root.right, sum - root.val);
}
双队列法:我们广度优先遍历树,用一个队列记录节点,用另一个队列记录遍历的路径值。
public static boolean pathSumQueue(TreeNode root, int sum) {
if (root == null) {
return false;
}
Queue<TreeNode> nodeQueue = new ArrayDeque<>();
Queue<Integer> sumQueue = new ArrayDeque<>();
nodeQueue.add(root);
sumQueue.add(root.val);
while (!nodeQueue.isEmpty()) {
int n = nodeQueue.size();
for (int i = 0; i < n; i++) {
TreeNode poll = nodeQueue.poll();
int currentValue = sumQueue.poll();
if (poll.right == null && poll.left == null) {
if (currentValue == sum){
return true;
}
}
if (poll.left != null) {
nodeQueue.add(poll.left);
sumQueue.add(poll.left.val + currentValue);
}
if (poll.right != null) {
nodeQueue.add(poll.right);
sumQueue.add(poll.right.val + currentValue);
}
}
}
return false;
}
3. 从中序遍历数组和有序遍历数组构造二叉树
原理:
* 1. 依据后序遍历的特点,可以得到数组最后一个元素作为根节点。
* 2.从中序遍历数组的特点和根节点,得到左右子树的个数,区间。
* 3.重复步骤1,2, 从中序遍历数组中递归地构造左右子树。
*/
int[] inOrder;
int[] postOrder;
Map<Integer,Integer> inorderMap = new HashMap<Integer, Integer>();
public TreeNode buildTree(int[] inOrder, int[] postOrder){
this.inOrder = inOrder;
this.postOrder = postOrder;
postRootIndex = postOrder.length - 1; //1. 后序遍历中的末节点是根节点
int inx = 0;
for (int val : inOrder) {
inorderMap.put(val, inx++); //缓存子序列中的节点和索引,有利于快速查询后序遍历中的根节点在中序遍历序列中的索引,此索引可以用来区分左右子树。
}
return helpInOrder(0, inOrder.length-1);//2. 从中序列表中,递归构造子右子树和左有子树
}
public static int postRootIndex = 0;
/**原理:
* 根据前序遍历序列和后序遍历序列来构建二叉树。
* 1. 从后序遍历中的特点,得到根节点
* 2.从中序遍历和根节点得到左右子树
* 3.重复1,2 从中序遍历中递归构造左右子树。
*/
public TreeNode helpInOrder(int inLeftIndex, int inRightIndex){
if (inLeftIndex > inRightIndex){ //递归的出口, 当起始指针索引大于末尾指针时,停止递归。
return null;
}
TreeNode root = new TreeNode(postOrder[postRootIndex]); //构造根节点
int inOrderRootIndex = inorderMap.get(postOrder[postRootIndex]); //得到根节点在中序遍历中的的索引,
postRootIndex --;//这里决定了必须先构造右子树
root.right = helpInOrder(inOrderRootIndex + 1, inRightIndex); // 递归构造右子树
root.left = helpInOrder(inLeftIndex, inOrderRootIndex -1);//递归构造左子树
return root;
}
4. 从前序与中序遍历序列构造二叉树
和上一题一个思路,不同点是用前序序列的第一个元素来确定根节点。然后从中序序列中递归构造左右子树。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int preRootIndex = 0;
int[] preOrder;
Map<Integer,Integer> inorderMap = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
this.preOrder = preorder;
int inX = 0;
for(int val : inorder) {
inorderMap.put(val, inX++);
}
return helpInOrderByRootFromPreOrder(0, preorder.length - 1);
}
public TreeNode helpInOrderByRootFromPreOrder(int inLeftIndex, int inRightIndex){
if (inLeftIndex > inRightIndex){ //递归的出口, 当起始指针索引大于末尾指针时,停止递归。
return null;
}
TreeNode root = new TreeNode(preOrder[preRootIndex]); //构造根节点
int inOrderRootIndex = inorderMap.get(preOrder[preRootIndex]); //得到根节点在中序遍历中的的索引,
preRootIndex++;//这里决定了必须先构造左子树
root.left = helpInOrderByRootFromPreOrder(inLeftIndex, inOrderRootIndex -1); // 递归构造左子树
root.right = helpInOrderByRootFromPreOrder(inOrderRootIndex + 1, inRightIndex);//递归构造右子树
return root;
}
}
5. 填充右侧节点next指针2
1.用层序遍历,队列法,会有o(n)的空间复杂度。
2.采用next链表法,遍历第x层时,来连接第x+1层的每个节点的next指针,链表法,空间复杂度o(1).
next链表法:
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
if(root == null) {
return null;
}
Node cur = root;//第x层 从第一层开始。
while(cur != null){
Node dummy = new Node(0);//第x+1层第一节点,假节点
Node pre = dummy;//第x+1层第一节点,假节点
//遍历同一行,并连接下一行
while(cur != null) {
if(cur.left != null) {
pre.next = cur.left;
pre = pre.next;
}
if(cur.right != null) {
pre.next = cur.right;
pre = pre.next;
}
cur = cur.next;
}
cur = dummy.next;
}
return root;
}
}
6.求两个节点最近公共祖先
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == q || root == p){
return root;//如果p或q等于root那么 root为最近公共祖先
}
TreeNode left = lowestCommonAncestor(root.left, p , q ); //
TreeNode right = lowestCommonAncestor(root.right,p, q);//
if(left == null) { return right;}//如果左子树捉不到p,q那么pq都在右子树,最先找到的p或q则是最近公共祖先。
if(right == null) {
return left;
}
return root;
}
}
7.求二叉搜索树的最大第k个节点值。
利用二叉搜索树的特性,把它中序遍历得到序列是一个递增数组,故求第k个大的节点,即中序遍历序列的倒序第k个树。
采用递归中序遍历的倒序遍历二叉树:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int k; int res;
public int kthLargest(TreeNode root, int k) {
this.k = k;
inOrder(root);
return res;
}
public void inOrder(TreeNode root){
if(root == null){
return;
}
inOrder(root.right);
if(k == 0) return;
if(--k == 0)res = root.val;
inOrder(root.left);
}
}
8. 求二叉搜索树的最近公共祖先
1.采用递归法, 如果p、q大于根节点,那么深度遍历右子树,返回root,同理如果pq小于根节点,递归遍历左子树,返回root
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
//root存在与右子树中
if (q.val > root.val && p.val > root.val){
root = lowestCommonAncestor(root.right,p, q);
}
//root存在于左子树中
if (p.val < root.val && q.val < root.val){
root = lowestCommonAncestor(root.left,p, q);
}
return root;
}
2.迭代法
1. 保证p小于q,p大于根节点,则遍历右子树, q小于根节点,则循环遍历左子树
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (p.val > q.val){
TreeNode temp = p;
p = q;
q = temp;
}
while (root != null){
if(p.val > root.val ){
root = root.right;
} else if (q.val < root.val) {
root = root. left;
} else break;
}
return root;
}
}
9. II. 平衡二叉树
难度简单122
输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。