(1)检查两棵树是否相同
题目链接:
. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/same-tree/
解题思路:
代码实现:
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
//两个节点都为空,相等
return true;
}
if (p != null && q != null) {
if (p.val == q.val) {
//如果结构和value都相等,递归剩下的节点,判断是否相等,直到
//p=q=null,说明遍历完成
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
return false;
}
//结构不相同,直接放回false
return false;
}
}
(2)另一颗树的子树
题目链接:. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/subtree-of-another-tree/
解题思路:
代码实现:
class Solution {
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;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
if (p != null && q != null) {
if (p.val == q.val) {
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
return false;
}
return false;
}
}
(3)翻转二叉树
题目链接:. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/invert-binary-tree/description/
解题思路:
代码实现:
class Solution {
public TreeNode invertTree(TreeNode root) {
TreeNode cur = root;
if (cur == null) {
return null;
}
TreeNode exchange = cur.left;
cur.left = root.right;
cur.right = exchange;
invertTree(cur.left);
invertTree(cur.right);
return root;
}
}
(4)判断一颗二叉树是否是平衡二叉树
题目链接:. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/balanced-binary-tree/description/
解题思路:
代码实现:
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null){
return true;
}
if(Math.abs(getHeight(root.left)-getHeight(root.right))<2){
//符合条件,继续遍历,直到root==null
return isBalanced(root.left) && isBalanced(root.right);
}else{
return false;
}
}
public int getHeight(TreeNode root){
//计算树的高度
if(root==null){
return 0;
}
return Math.max(getHeight(root.left),getHeight(root.right))+1;
}
}
(5)对称二叉树
题目链接:. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/symmetric-tree/
解题思路:
代码实现:
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
if(judgeIsSymmetric(root.left,root.right)){
return true;
}
return false;
}
public boolean judgeIsSymmetric(TreeNode p,TreeNode q){
//判断是否对称二叉树
if(p==null && q==null){
return true;
}else if(p!=null && q!=null){
if(p.val==q.val){
//val值相等才可以继续递归,比较剩下的节点
return judgeIsSymmetric(p.left,q.right) && judgeIsSymmetric(p.right,q.left);
}
}
return false;
}
}
(6)二叉树的构建及遍历
题目链接:
二叉树遍历_牛客题霸_牛客网编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。题目来自【牛客题霸】https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId=60&&tqId=29483&rp=1&ru=/activity/oj&qru=/ta/tsing-kaoyan/question-ranking
解题思路:
代码实现:
import java.util.Scanner;
class Node {
public char val;
public Node left;
public Node right;
public Node(char val){
this.val=val;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String str = in.nextLine();
Node node = createTree(str);
inOrder(node);
}
}
public static int i=0;
public static Node createTree(String str) {
Node root=null;
if(str.charAt(i)!='#'){
root=new Node(str.charAt(i));
i++;
root.left=createTree(str);
root.right=createTree(str);
}else{
i++;
}
return root;
}
public static void inOrder(Node node){
if(node==null){
return;
}
inOrder(node.left);
System.out.print(node.val+" ");
inOrder(node.right);
}
}
(7)二叉树的分层遍历
题目链接:
. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/binary-tree-level-order-traversal/description/
解题思路:
代码实现:
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) {
return new ArrayList();
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
List<List<Integer>> list = new LinkedList<>();
while (!queue.isEmpty()) {
List<Integer> list1 = new LinkedList<>();
int size = queue.size();
//每一次大循环都计算一下队列的大小,保证每一层节点的数值存放在不同的表中
while (size-- > 0) {
TreeNode cur = queue.poll();
list1.add(cur.val);//放入本层所有的值
System.out.println(cur.val + " ");
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
//每次大循环末尾,将子表放入主表中
list.add(list1);
}
return list;
}
}
(8)给定一个二叉树, 找到该树中两个指定节点的最近公共祖先
题目链接:
. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/
解题思路:
代码实现:
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==null){
return null;
}
//根结点为p或者q,公共祖先就是根结点
if(root == p || root == q){
return root;
}
TreeNode ret1 = lowestCommonAncestor(root.left,p,q);
TreeNode ret2 = lowestCommonAncestor(root.right,p,q);
//如果ret1和ret2都不为空,则root为公共祖先
if(ret1!=null && ret2!=null){
return root;
}else if(ret1!=null){
return ret1;
}else{
return ret2;
}
}
}
(9)二叉搜索树与双向链表
题目链接:二叉搜索树与双向链表_牛客题霸_牛客网输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。如下图所示。题目来自【牛客题霸】https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tqId=11179&ru=/exam/oj
解题思路:
代码实现:
public class Solution {
TreeNode prev = null;
public void ConvertChild(TreeNode pRootOfTree) {
if (pRootOfTree == null) {
return ;
}
ConvertChild(pRootOfTree.left);
pRootOfTree.left = prev;
if (prev != null) {
prev.right = pRootOfTree;
}
prev = pRootOfTree;
ConvertChild(pRootOfTree.right);
}
public TreeNode Convert(TreeNode pRootOfTree) {
if (pRootOfTree == null) {
return null;
}
ConvertChild(pRootOfTree);
TreeNode head = pRootOfTree;
while (head.left != null) {
head = head.left;
}
return head;
}
}