所有题目基于如下树节点:
public class TreeNode {
int val;
TreeNode left = null;
TreeNode right = null;
TreeNode(int x) { val = x; }
}
题目合集:
- 先序序列和中序序列重建二叉树
- 判断一棵树是否为另一颗树的子结构
- 二叉树的镜像
- 层级遍历二叉树
- 判断一个序列是否为某二叉搜索树的后序序列
- 二叉树的所有路径和为某一值
先序序列和中序序列重建二叉树
/**
* Created by lrx on 2017/4/9.
*/
// 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树
public class ReConstructBinaryTree {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
return reConstructBinaryTree(pre, 0, pre.length-1, in, 0, in.length-1);
}
private static TreeNode reConstructBinaryTree(int[] pre, int l1, int r1, int[] in, int l2, int r2) {
if (l1 > r1 || l2 > r2) return null;
TreeNode root = new TreeNode(pre[l1]);
int index = l2;
while (index <= r2 && in[index] != pre[l1]) {
index++;
}
if (index > r2) return null;
root.left = reConstructBinaryTree(pre, l1+1, l1+index-l2, in, l2, index-1);
root.right = reConstructBinaryTree(pre, l1+index-l2+1, r1, in, index+1, r2);
return root;
}
}
判断一棵树是否为另一颗树的子结构
/**
* Created by lrx on 2017/4/10.
*/
// 判断一棵树是否是另一颗树的子结构
public class JudgeSubTree {
public static void main(String[] args) {
TreeNode root1 = new TreeNode(8);
root1.left = new TreeNode(6);
root1.right = new TreeNode(7);
TreeNode root2 = new TreeNode(8);
JudgeSubTree j = new JudgeSubTree();
boolean r = j.HasSubtree(root1, root2);
System.out.println(r);
}
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if (root1 ==null || root2 == null)
return false;
return preOrder(root1,root2);
}
// 先序遍历找到第一个相同的节点
private boolean res = false;
private boolean preOrder(TreeNode root1, TreeNode root2) {
if (root1 == null) return false;
if (root1.val == root2.val) {
res = judge(root1,root2);
if (res)
return true;
}
return preOrder(root1.left,root2) || preOrder(root1.right,root2);
}
// 递归判断两个树是否相等
private boolean judge(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) return true;
if (root1 == null) return false;
if (root2 == null) return true;
if (root1.val == root2.val) {
return judge(root1.left,root2.left) && judge(root1.right,root2.right);
} else {
return false;
}
}
}
二叉树的镜像
/**
* Created by lrx on 2017/4/10.
*/
// 求一棵树的镜像
public class MirrorOfTree {
public void Mirror(TreeNode root) {
if (root == null) return;
TreeNode p = root.left;
root.left = root.right;
root.right = p;
Mirror(root.left);
Mirror(root.right);
}
}
层级遍历二叉树
/**
* Created by lrx on 2017/4/11.
*/
// 层级遍历,用队列,类似广度优先遍历
public class TreeFloorPrint {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
if (root == null) return res;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode p = queue.removeFirst();
res.add(p.val);
if (p.left != null)
queue.add(p.left);
if (p.right != null)
queue.add(p.right);
}
return res;
}
}
判断一个序列是否为某二叉搜索树的后序序列
/**
* Created by lrx on 2017/4/11.
*/
// 验证一个序列是一颗二叉搜索树的后序遍历序列
// 只需递归判断子树也是二叉搜索树即可
public class VerifySquenceIsBST {
public static void main(String[] args) {
VerifySquenceIsBST v = new VerifySquenceIsBST();
v.VerifySquenceOfBST(new int[] {2,4,3,6,8,7,5});
}
public boolean VerifySquenceOfBST(int [] sequence) {
if (sequence.length <= 1) return true;
return verify(sequence, 0, sequence.length-1);
}
private boolean verify(int[] seq, int l1, int r1) {
if ((r1-l1) <= 0) return true;
int root = seq[r1];
int index = l1;
while (index < r1 && seq[index] < root) {
index++;
}
for (int i=index; i<r1; i++) {
if (seq[i] < root)
return false;
}
return verify(seq, l1, index-1) && verify(seq, index, r1-1);
}
}
二叉树的所有路径和为某一值
/**
* Created by lrx on 2017/4/11.
*/
// 二叉树所有路径之和为某值,采用递归实现
public class TreeAllPath {
public static void main(String[] args) {
TreeAllPath t = new TreeAllPath();
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(12);
ArrayList<ArrayList<Integer>> res = t.FindPath(root, 22);
System.out.println(res);
}
ArrayList<ArrayList<Integer>> res;
ArrayList<Integer> aPath;
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
res = new ArrayList<>();
if (root == null) return res;
aPath = new ArrayList<>();
findPath(root, target);
return res;
}
private void findPath(TreeNode root, int target) {
if (root == null) return;
aPath.add(root.val);
// 是叶子结点
if (root.left == null && root.right == null) {
int sum = 0;
for (Integer i : aPath) {
sum += i;
}
if(sum == target) {
// res.add(aPath);这样不行,可能因为res持有apath的引用,而之后aPath还会改变
ArrayList<Integer> r = new ArrayList<>();
for (Integer i : aPath) {
r.add(i);
}
res.add(r);
}
aPath.remove(aPath.size()-1);
return;
}
findPath(root.left, target);
findPath(root.right, target);
aPath.remove(aPath.size()-1);
}
}