(1)检查两棵树是否相同
题目链接:
. - 力扣(LeetCode). - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。
https://leetcode.cn/problems/same-tree/
解题思路:
![](https://i-blog.csdnimg.cn/direct/ad40b36787dd441fa12869df0b6bb2f3.png)
代码实现:
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)另一颗树的子树
解题思路:
![](https://i-blog.csdnimg.cn/direct/7ae9bb26d3be49eca9e749dddf01a278.png)
代码实现:
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)翻转二叉树
解题思路:
![](https://i-blog.csdnimg.cn/direct/559f7486ddd14fc5a453d6286629dbb5.png)
代码实现:
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)判断一颗二叉树是否是平衡二叉树
解题思路:
![](https://i-blog.csdnimg.cn/direct/3a476cf402594e008eb33d85b8412490.png)
代码实现:
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)对称二叉树
解题思路:
![](https://i-blog.csdnimg.cn/direct/4e9b155b70d749cbbf3951e25e9943e6.png)
代码实现:
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
解题思路:![](https://i-blog.csdnimg.cn/direct/85939633628c48a9aaab900bb133b33b.png)
代码实现:
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/
解题思路:
![](https://i-blog.csdnimg.cn/direct/25b390abd14f43df939f46abe37ef276.png)
代码实现:
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/
解题思路:
![](https://i-blog.csdnimg.cn/direct/76d32b11f0b441a599483507f25f62f8.png)
代码实现:
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://i-blog.csdnimg.cn/direct/e8147c46f0c8412193e0779720662bc0.png)
代码实现:
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;
}
}