目录
[给定一个二叉树, 找到该树中两个指定节点的最近公共祖先](#给定一个二叉树, 找到该树中两个指定节点的最近公共祖先)
检查两棵树是否相同
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// 1.一个为空 一个不为空[结构上不一样]
if (p != null && q == null || p == null && q != null) {
return false;
}
// 2.第二步走完之后,要么都为空,要么都不为空 两个都是空
if (p == null && q == null) {
return true;
}
// 3.都不为空
if (p.val != q.val) {
return false;
}
// 4.此时代表2个都不为空 同时val值也是一样的
// 5.说明根节点相同,接下来判断两棵树的左和两棵树的右是不是同时相同
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
另一颗树的子树
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// 1.一个为空 一个不为空[结构上不一样]
if (p != null && q == null || p == null && q != null) {
return false;
}
// 2.第二步走完之后,要么都为空,要么都不为空 两个都是空
if (p == null && q == null) {
return true;
}
// 3.都不为空
if (p.val != q.val) {
return false;
}
// 4.此时代表2个都不为空 同时val值也是一样的
// 5.说明根节点相同,接下来判断两棵树的左和两棵树的右是不是同时相同
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null) {
return false;
}
if (isSameTree(root, subRoot)) {
return true;
}
if (isSubtree(root.left, subRoot)) {
return true;
}
if (isSubtree(root.right, subRoot)) {
return true;
}
return false;
}
}
翻转二叉树
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
//减少一些递归和交换的次数
if (root.left == null && root.right == null) {
return root;
}
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
invertTree(root.left);
invertTree(root.right);
return root;
}
}
判断一颗二叉树是否是平衡二叉树
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
int leftH = maxDepth(root.left);
int rightH = maxDepth(root.right);
return Math.abs(leftH - rightH) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
}
但这样会有重复的计算,时间复杂度高,可优化为:
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
return maxDepth(root) >= 1;
}
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = maxDepth(root.left);
if (leftHeight < 0) {
return -1;
}
int rightHeight = maxDepth(root.right);
if (rightHeight < 0) {
return -1;
}
if (Math.abs(leftHeight - rightHeight) <= 1) {
return Math.max(leftHeight, rightHeight) + 1;
} else {
return -1;
}
}
}
对称二叉树
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
return isSymmetricChild(root.left, root.right);
}
public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree) {
// 1.检查结构是否相同->一个为空 一个不为空
if (leftTree != null && rightTree == null || leftTree == null && rightTree != null) {
return false;
}
// 2.检查结构是否相同->处理【两个都为空】
if (leftTree == null && rightTree == null) {
return true;
}
// 3.检查结构是否相同->处理【两个都不为空,判断值一样吗】
if (leftTree.val != rightTree.val) {
return false;
}
// 4.此时两个引用都不为空 同时节点的值一样
// 5.开始判断是否对称
// 6.满足左子树的左和右子树的右对称 同时左子树的右和右子树的左对称
return isSymmetricChild(leftTree.left, rightTree.right) && isSymmetricChild(leftTree.right, rightTree.left);
}
}
二叉树的构建及遍历
java
import java.util.Scanner;
class TreeNode {
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static int i = 0;
public static TreeNode createTree(String str) {
TreeNode root = null;
if (str.charAt(i) != '#') {
root = new TreeNode(str.charAt(i));
i++;
root.left = createTree(str);
root.right = createTree(str);
} else {
i++;
}
return root;
}
public static void inorder(TreeNode root) {
if (root == null) {
return;
}
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
TreeNode root = createTree(str);
inorder(root);
}
}
}
二叉树的分层遍历
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if (root == null) {
return ret;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();// 每层
while (size > 0) {
TreeNode cur = queue.poll();
list.add(cur.val);
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
size--;
}
ret.add(list);
}
return ret;
}
}
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先
java
/**
* 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) {
return null;
}
if (root == p || root == q) {
return root;
}
TreeNode leftTree = lowestCommonAncestor(root.left, p, q);
TreeNode rightTree = lowestCommonAncestor(root.right, p, q);
// 两边都不为空
if (leftTree != null && rightTree != null) {
return root;
} else if (leftTree != null) {
// 左边不为空
return leftTree;
} else {
// 右边不为空
return rightTree;
}
}
}
java
/**
* 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) {
return root;
}
// 1.获取root到指定节点路径上的所有结点
Stack<TreeNode> stackP = new Stack<>();
getPath(root, p, stackP);
Stack<TreeNode> stackQ = new Stack<>();
getPath(root, q, stackQ);
// 2.上述代码执行完成 对应栈当中 存储了指定路径上的所有节点
int sizeP = stackP.size();
int sizeQ = stackQ.size();
if (sizeP > sizeQ) {
int size = sizeP - sizeQ;
while (size != 0) {
stackP.pop();
size--;
}
} else {
int size = sizeQ - sizeP;
while (size != 0) {
stackQ.pop();
size--;
}
}
// 3.上述if语句执行完成之后 两个栈当中的元素个数是一样的
while (!stackP.isEmpty() && !stackQ.isEmpty()) {
if (stackP.peek() == stackQ.peek()) {
return stackP.peek();
} else {
stackP.pop();
stackQ.pop();
}
}
return null;
}
public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
if (root == null) {
return false;
}
stack.push(root);
if (root == node) {
return true;
}
boolean flg = getPath(root.left, node, stack);
if (flg) {
return true;
}
boolean flg2 = getPath(root.right, node, stack);
if (flg2) {
return true;
}
stack.pop();
return false;
}
}
根据一棵树的前序遍历与中序遍历构造二叉树
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int preIndex;
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeChilde(preorder, inorder, 0, inorder.length - 1);
}
private TreeNode buildTreeChilde(int[] preorder, int[] inorder, int inBegin, int inEnd) {
if (inBegin > inEnd) {
return null;
}
TreeNode root = new TreeNode(preorder[preIndex]);
int rootIndex = findRootIndex(inorder, inBegin, inEnd, preorder[preIndex]);
preIndex++;
root.left = buildTreeChilde(preorder, inorder, inBegin, rootIndex - 1);
root.right = buildTreeChilde(preorder, inorder, rootIndex + 1, inEnd);
return root;
}
private int findRootIndex(int[] inorder, int inBegin, int inEnd, int key) {
for (int i = inBegin; i <= inEnd; i++) {
if (inorder[i] == key) {
return i;
}
}
return -1;
}
}
根据一棵树的中序遍历与后序遍历构造二叉树
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int postIndex = 0;
public TreeNode buildTree(int[] inorder, int[] postorder) {
postIndex = postorder.length - 1;
return buildTreeChild(inorder, postorder, 0, inorder.length - 1);
}
private TreeNode buildTreeChild(int[] inorder, int[] postorder, int inBegin, int inEnd) {
if (inBegin > inEnd) {
return null;
}
TreeNode root = new TreeNode(postorder[postIndex]);
int rootIndex = findRootIndex(inorder, inBegin, inEnd, postorder[postIndex]);
postIndex--;
root.right = buildTreeChild(inorder, postorder, rootIndex + 1, inEnd);
root.left = buildTreeChild(inorder, postorder, inBegin, rootIndex - 1);
return root;
}
private int findRootIndex(int[] inorder, int inBegin, int inEnd, int key) {
for (int i = inBegin; i <= inEnd; i++) {
if (inorder[i] == key) {
return i;
}
}
return -1;
}
}
二叉树创建字符串
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public String tree2str(TreeNode root) {
StringBuilder stringBuilder = new StringBuilder();
tree2strChild(root, stringBuilder);
return stringBuilder.toString();
}
private void tree2strChild(TreeNode root, StringBuilder stringBuilder) {
if (root == null)
return;
stringBuilder.append(root.val);
if (root.left != null) {
stringBuilder.append("(");
tree2strChild(root.left, stringBuilder);
stringBuilder.append(")");
} else {
// 左边为空 右边也为空
if (root.right == null) {
return;
} else {
stringBuilder.append("()");
}
}
// 处理root右树的情况
if (root.right == null) {
return;
} else {
stringBuilder.append("(");
tree2strChild(root.right, stringBuilder);
stringBuilder.append(")");
}
}
}