1.二叉树的前序中序后序遍历
输出二叉树的前序,中序,后序遍历
图片参考: 二叉树前序遍历、中序遍历、后序遍历、层序遍历的直观理解 - 只争朝夕,不负韶华! - 博客园
1.对称二叉树:
题目描述
请实现一个函数,用来判断一棵二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
public class Solution {
boolean isSymmetrical(TreeNode pRoot) {
if(pRoot == null){
return true;
}
return doSymmetrial(pRoot.left, pRoot.right);
}
boolean doSymmetrial(TreeNode p, TreeNode q){
if(p == null && q != null || p != null && q == null){
return false;
}
if(p != null && q != null && p.val != q.val){
return false;
}
if(p == null && q == null){
return true;
}
boolean left = doSymmetrial(p.left, q.right);
boolean right = doSymmetrial(p.right, q.left);
return left && right;
}
}
2.二叉树的镜像:
操作给定的二叉树,将其变换为源二叉树的镜像。
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot == null){
return null;
}
Mirror(pRoot.left);
Mirror(pRoot.right);
TreeNode temp = pRoot.left;
pRoot.left = pRoot.right;
pRoot.right = temp;
return pRoot;
}
}
使用前序遍历的方式:
使用后序遍历的方式:
import java.util.*;
public class Solution {
public TreeNode Mirror (TreeNode pRoot) {
if(pRoot == null){
return null;
}
TreeNode temp = pRoot.left;
pRoot.left = pRoot.right;
pRoot.right = temp;
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
}
3.二叉树的深度
题目描述
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
4.平衡二叉树(备注:左右子树的高度差就是树的深度)
具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.(以下讲解来自牛客网)
这道题目其实跟二叉树的深度这道题用到的方法是一样的,为什么说是一样的呢?因为我们求二叉树的深度,其实就是求了左右子树的深度的最大值,但是这道题目是要让我们判断二叉树是不是平衡树。
我们都知道如何判断一棵二叉树是不是平衡二叉树,就是它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
所以,这个时候我们只需要判断左右子树的深度之差有没有超过1,若超过了则不是平衡的,反之则为平衡二叉树。
public int height(TreeNode node){
if(node == null){
return 0;
}
int left = height(node.left);
int right = height(node.right);
if(Math.abs(left - right) > 1){
res = false;
}
return Math.max(left, right) + 1;
}
但是,上面的代码总是遍历完全部的节点,我们想想,如果一判断到左右子树的深度之差大于1,即这个二叉树就不可能再是平衡树了。
所以,我们还可以对上面代码进行优化。
进行剪枝:当判断到左右子树的深度之差大于1的时候,则返回-1。每次递归结束判断返回值是否-1,若为-1,则立即返回。
public class Solution {
boolean res = true;
public boolean IsBalanced_Solution(TreeNode root) {
if(root == null){
return true;
}
height(root);
return res;
}
public int height(TreeNode node){
if(node == null){
return 0;
}
int left = height(node.left);
if(left == -1){
return -1;
}
int right = height(node.right);
if(right == -1){
return -1;
}
if(Math.abs(left - right) > 1){
res = false;
return -1;
}
return Math.max(left, right) + 1;
}
}
5.二叉树中和为某一值的路径
题目描述
输入一颗二叉树的根节点和一个整数,按字典序打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
public class Solution {
ArrayList<Integer> listSum = new ArrayList<>();
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root == null){
return new ArrayList<>();
}
listSum.add(root.val);
target = target - root.val;
if(root.left == null && root.right == null && target == 0){
res.add(new ArrayList<>(listSum));
}
FindPath(root.left, target);
FindPath(root.right, target);
listSum.remove(listSum.size() - 1);
return res;
}
}
这题是回溯法
6.自上而下打印二叉树
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
import java.util.*;
public class Solution {
ArrayList<Integer> result = new ArrayList<>();
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
if(root == null){
return result;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.addFirst(root);
while(!queue.isEmpty()){
root = queue.removeLast();
result.add(root.val);
if(root.left != null){
queue.addFirst(root.left);
}
if(root.right != null){
queue.addFirst(root.right);
}
}
return result;
}
}
思路:这题是树的广度遍历,使用一个队列进行辅助求解
7.把二叉树打印成多行
题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
输入:
{8,6,10,5,7,9,11}
返回值:
[[8],[6,10],[5,7,9,11]]
import java.util.*;
public class Solution {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
ArrayList<ArrayList<Integer> > Print(TreeNode root) {
if(root == null){
return new ArrayList();
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.addFirst(root);
int now = 1;
int next = 0;
ArrayList<Integer> temp = new ArrayList<>();
while(!queue.isEmpty()){
root = queue.removeLast();
temp.add(root.val);
now--;
if(root.left != null){
next++;
queue.addFirst(root.left);
}
if(root.right != null){
next++;
queue.addFirst(root.right);
}
if(now == 0){
now = next;
result.add(new ArrayList(temp));
temp.clear();
next = 0;
}
}
return result;
}
}
8.二叉树的查找
二叉树的查找_chenkaibsw的博客-CSDN博客_二叉树的查找
9.判断是不是完全二叉树
描述:
给定一个二叉树,确定他是否是一个完全二叉树。
完全二叉树的定义:若二叉树的深度为 h,除第 h 层外,其它各层的结点数都达到最大个数,第 h 层所有的叶子结点都连续集中在最左边,这就是完全二叉树。(第 h 层可能包含 [1~2h] 个节点)
这里借助一个队列进行操作
代码实现:
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
public boolean isCompleteTree (TreeNode root) {
// write code here
// write code here
if(root == null){
return true;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
boolean flag = false;
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node == null){
flag = true;
continue;
}
if(flag){
return false;
}
queue.offer(node.left);
queue.offer(node.right);
}
return true;
}
}
10.二叉搜索树的最近公共祖先
思路一:查询5,将遍历过程记录下来7 4 6 5 ,查询9,将遍历过程记录下来7 10 9。
倒序查找第一个相同的节点为7。该节点就是最近公共祖先。
public class Solution {
public int lowestCommonAncestor (TreeNode root, int p, int q) {
// write code here
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
findValue(root, p, list1);
findValue(root, q, list2);
return findCommon(list1, list2);
}
public int findCommon(List<Integer> list1, List<Integer> list2){
for(int i = list1.size() - 1; i >= 0; i--){
if(list2.contains(list1.get(i))){
return list1.get(i);
}
}
return -1;
}
public void findValue(TreeNode root, int value, List<Integer> list){
if(root == null){
return;
}
list.add(root.val);
if(root.val == value){
return;
}
if(root.val < value){
findValue(root.right, value, list);
}
if(root.val > value){
findValue(root.left, value, list);
}
}
}
思路二:使用递归的方式:
public class Solution {
public int lowestCommonAncestor (TreeNode root, int p, int q) {
// write code here
return findValue(root, p, q);
}
public int findValue(TreeNode root, int p, int q){
if(root == null){
return -1;
}
if(root.val <= p && root.val >= q || root.val <= q && root.val >= p){
return root.val;
}
int left = findValue(root.left, p, q);
int right = findValue(root.right, p, q);
if(left != -1){
return left;
}
if(right != -1){
return right;
}
return -1;
}
}
11.合并二叉树
已知两颗二叉树,将它们合并成一颗二叉树。合并规则是:都存在的结点,就将结点值加起来,否则空的位置就由另一个树的结点来代替。
public class Solution {
boolean isSymmetrical(TreeNode pRoot) {
if(pRoot == null){
return true;
}
return doSymmetrial(pRoot.left, pRoot.right);
}
boolean doSymmetrial(TreeNode p, TreeNode q){
if(p == null && q != null || p != null && q == null){
return false;
}
if(p != null && q != null && p.val != q.val){
return false;
}
if(p == null && q == null){
return true;
}
boolean left = doSymmetrial(p.left, q.right);
boolean right = doSymmetrial(p.right, q.left);
return left && right;
}
}
12.在二叉树中找到两个节点的最近公共节点
描述
给定一棵二叉树(保证非空)以及这棵树上的两个节点对应的val值 o1 和 o2,请找到 o1 和 o2 的最近公共祖先节点。
数据范围:树上节点数满足 1 \le n \le 10^5 \1≤n≤105 , 节点值val满足区间 [0,n)
要求:时间复杂度 O(n)O(n)
注:本题保证二叉树中每个节点的val值均不相同。
如当输入{3,5,1,6,2,0,8,#,#,7,4},5,1时,二叉树{3,5,1,6,2,0,8,#,#,7,4}如下图所示:
所以节点值为5和节点值为1的节点的最近公共祖先节点的节点值为3,所以对应的输出为3。
节点本身可以视为自己的祖先。
我们也可以讨论几种情况:
- step 1:如果o1和o2中的任一个和root匹配,那么root就是最近公共祖先。
- step 2:如果都不匹配,则分别递归左、右子树。
- step 3:如果有一个节点出现在左子树,并且另一个节点出现在右子树,则root就是最近公共祖先.
- step 4:如果两个节点都出现在左子树,则说明最低公共祖先在左子树中,否则在右子树。
- step 5:继续递归左、右子树,直到遇到step1或者step3的情况。
代码实现:
import java.util.*;
public class Solution {
public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
//该子树没找到,返回-1
if(root == null)
return -1;
//该节点是其中某一个节点
if(root.val == o1 || root.val == o2)
return root.val;
//左子树寻找公共祖先
int left = lowestCommonAncestor(root.left, o1, o2);
//右子树寻找公共祖先
int right = lowestCommonAncestor(root.right, o1, o2);
//左子树为没找到,则在右子树中
if(left == -1)
return right;
//右子树没找到,则在左子树中
if(right == -1)
return left;
//否则是当前节点
return root.val;
}
}
13.判断是不是二叉搜索树
描述
给定一个二叉树根节点,请你判断这棵树是不是二叉搜索树。
二叉搜索树满足每个节点的左子树上的所有节点均严格小于当前节点且右子树上的所有节点均严格大于当前节点。
二叉搜索树 非二叉搜索树
思路: 二叉搜索树的中序遍历是递增的。
代码实现:
public class Solution {
/**
* @param root TreeNode类
* @return bool布尔型
*/
boolean flag = false;
int temp = Integer.MIN_VALUE;
public boolean isValidBST (TreeNode root) {
// write code here
if(root == null){
return false;
}
doVaidBST(root);
return !flag;
}
public void doVaidBST(TreeNode root){
if(root == null || flag){
return;
}
doVaidBST(root.left);
if(root.val > temp){
temp = root.val;
}else{
flag = true;
}
doVaidBST(root.right);
}
}