二叉树操作合集(陆续更新)
来源:互联网 发布:洗衣烘干一体机 知乎 编辑:程序博客网 时间:2024/06/05 09:24
所有题目基于如下树节点:
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); }}
0 0
- 二叉树操作合集(陆续更新)
- QT编程开发错误合集陆续更新
- es基本操作(陆续更新)
- mongodb基础操作(陆续更新)
- 面试(陆续更新)
- 好书不断(陆续更新)
- CSS 兼容性(陆续更新)
- PHP注意事项(陆续更新)
- 前端资源(陆续更新)
- Android_Service 笔记(陆续更新)
- 面试题(陆续更新)
- docker基础(陆续更新)
- IOS-文件操作合集-待更新
- 二叉树问题合集
- 二叉树的操作(后续更新)
- Ubuntu操作(陆续添加)
- C#窗口之间的控制及数据操作(陆续更新)
- Pein线段树总结(陆续更新ing)
- MFC中定时器使用实例
- remind 一个问号两小时 :)
- muduo-源码分析2:注册监听socket和建立连接
- Openstack Ocata安装(Identity service)
- android:screenOrientation属性
- 二叉树操作合集(陆续更新)
- 编程总结
- 文章标题
- more命令
- centeros 安装 ssh
- delphi xe 解析复杂json
- 将二叉树拆成链表
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- Random 数字 | 时间随机