给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。(将中序遍历树,在遍历)
import java.util.*;
public class Solution {
TreeNode KthNode(TreeNode pRoot, int k) {
List list = new ArrayList<>();
KthNode(pRoot, list);
if (k<=0||k>list.size()){
return null;
}
return new TreeNode(list.get(k-1));
}
//中序遍历 是按照大小的排列的
public void KthNode(TreeNode root, List list) {
//中序遍历 左右根
if (root == null) {
return;
}
if (root.left != null) {
KthNode(root.left, list);
}
list.add(root.val);
if (root.right != null) {
KthNode(root.right, list);
}
}
}
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。(之字打印)
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
ArrayList> Print(TreeNode pRoot) {
ArrayList> ret = new ArrayList>();
//存放每一个层的数据
ArrayList tmp = new ArrayList();
//存放的树中的节点的队列
LinkedList q = new LinkedList();
if (pRoot == null)
return ret;
q.add(pRoot);
int now = 1, next = 0;
while (!q.isEmpty()) {
TreeNode t = q.remove();
now--;
tmp.add(t.val);
if (t.left != null) {
q.add(t.left);
next++;
}
if (t.right != null) {
q.add(t.right);
next++;
}
if (now == 0) {
//存放的是一行的数据
ret.add(new ArrayList(tmp));
//清空第一行的数据
tmp.clear();
now = next;
//赋值为0
next = 0;
}
}
return ret;
}
}
请实现一个函数,用来判断一棵二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
return isMirror(pRoot,pRoot);
}
public boolean isMirror(TreeNode t1, TreeNode t2) {
//如果是叶子节点的话,那么就是的返回相同
if (t1 == null && t2 == null) return true;
// 如果有其中等于null另外一个不等于空的话,返回是false
if (t1 == null || t2 == null) return false;
//判断是节点的值是否相同 并判断左子树 和右子树是否相同
return (t1.val == t2.val)&& isMirror(t1.right, t2.left)&& isMirror(t1.left, t2.right);
}
}
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
//判断首位平衡二叉树的就是判断左右子树的深度是否大于2?
return recur(root) != -1;
}
public int recur(TreeNode root) {
//如果等于的null返回是的0
if (root == null) return 0;
//计算左子树的长度
int left = recur(root.left);
if(left == -1) return -1;
//计算右子树的长度
int right = recur(root.right);
if(right == -1) return -1;
//判断左右子树的长度是否大于2?
return Math.abs(left - right) < 2 ? Math.max(left, right) + 1 : -1;
}
}
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.math.*;
public class Solution {
public int TreeDepth(TreeNode root) {
//如果节点是null返回为0
if(root == null) return 0;
//计算左子树的深度
int left=TreeDepth(root.left);
//计算右子树的深度
int ringht=TreeDepth(root.right);
//返回的左右子树的大的值的加上1
return Math.max(left,ringht) + 1;
}
}
输入一颗二叉树的根节点和一个整数,按字典序打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
import java.util.*;
public class Solution {
// 全局变量,用于存储得到的每一个路径
ArrayList> resultsList = new ArrayList>();
/**
* 建立额外一个函数,用来实现递归求解
* @param root
* @param target
* @return
*/
public ArrayList> FindPath(TreeNode root,int target) {
ArrayList pathList = new ArrayList();
if (root == null) {
return resultsList;
}
int curSum = 0;
int index = 0;
int []path = new int[1000];
this.isTargetPath(root, target, curSum, path, index);
return this.resultsList;
}
/**
* 递归求解函数
* 思路很明白,把根节点到叶节点的路径上的值都加起来,
* 所以在递归的过程中,需要逐个累加,并且累加的同时,还要将沿途经过的节点值记录下来,
* 如何在递归函数中实现这一切功能呢?参数!!可用传参的方式解决
* @param eleNode 当前节点
* @param target 目标和
* @param curSum 当前已经累积到的和
* @param path 记录到当前的节点位置,经过的路径
* @param index 从根节点到当前节点为止,存的节点的数目
*/
public void isTargetPath(TreeNode eleNode, int target, int curSum, int []path, int index) {
if (eleNode == null) {
return;
}
curSum += eleNode.val;
// 把该节点包含进去
path[index] = eleNode.val;
index ++;
//当前已经是处于叶子节点,并且累计的和与target相等
if (curSum == target && eleNode.left == null && eleNode.right == null) {
// 将得到的结果放在外层结构中
ArrayList pathList = new ArrayList();
for (int i = 0; i < index; i++) {
pathList.add(path[i]);
}
resultsList.add(pathList);
return;
}
// 该节点有左子节点,前提还是要curSum 小于 target,否则递归就没有意义了
if (curSum < target && eleNode.left != null) {
this.isTargetPath(eleNode.left, target, curSum, path, index);
}
// 右子节点
if (curSum < target && eleNode.right != null) {
this.isTargetPath(eleNode.right, target, curSum, path, index);
}
}
}
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
import java.util.ArrayList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
public class Solution {
//这个题目就是相当于是一个层序的遍历
public ArrayList PrintFromTopToBottom(TreeNode root) {
ArrayList list = new ArrayList();
if(root == null) return list;
Deque deque = new LinkedList();
deque.add(root);
while(!deque.isEmpty()){
TreeNode t = deque.pop();
list.add(t.val);
if(t.left != null) deque.add(t.left);
if(t.right != null) deque.add(t.right);
}
return list;
}
}
操作给定的二叉树,将其变换为源二叉树的镜像。
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public void Mirror(TreeNode root) {
//如果是空的返回是null
if (root == null) {
return;
}
//节点的交换
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
//递归到左子树
Mirror(root.left);
//递归到右子树
Mirror(root.right);
}
}
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public boolean HasSubtree(TreeNode root1, TreeNode root2) {
//如果是一种一个等于空的话返回是的false
if (root1 == null || root2 == null) {
return false;
}
//否则就调用函数的 分别是去判断时候的根节点 或者是的左子树的节点 或者是右节点的的节点
return help(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
}
private boolean help(TreeNode root1, TreeNode root2) {
//如果是2比较完成;那么就返回true
if (root2 == null) {
return true;
}
//如果是1为空;那么就返回false
if (root1 == null) {
return false;
}
//比较是的更加的值时候相同
return root1.val == root2.val && help(root1.left, root2.left) && help(root1.right, root2.right);
}
}
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.Arrays;
public class Solution {
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
//递归函数的出口
if (pre == null || pre.length == 0) {
return null;
}
TreeNode root = new TreeNode(pre[0]);
int index = findindex(pre, in);
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, index + 1), Arrays.copyOfRange(in, 0, index));
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre, index + 1, pre.length), Arrays.copyOfRange(in, index + 1, in.length));
return root;
}
public int findindex(int[] pre, int[] in) {
for (int i = 0; i < in.length; i++) {
if (in[i] == pre[0]) {
return i;
}
}
return -1;
}
}
翻转二叉树
public TreeNode reveserTree(TreeNode root) {
if(root==null){
return null;
}
reveserTree(root.left);
reveserTree(root.right);
//翻转
TreeNode temp=root.left;
root.left=root.right;
root.right=tmep;
return root;
}