算法——树(10%)
1,把二元查找树转变成排序的双向链表(树)
2,在二元树中找出和为某一值的所有路径(树)
3, 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。
4, 求二叉树中节点的最大距离
5, 输入一颗二元查找树,将该树转换为它的镜像,
即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
用递归和循环两种方法完成树的镜像转换。
例如输入:
8
/\
6 10
/\/\
5 7 9 11
输出:
8
/ \
10 6
/\/\
11 9 7 5
static void frontOrderMirror(Node root){if (root == null) return;Node p;p = root.LeftChild;root.LeftChild = root.RightChild;root.RightChild = p;frontOrderMirror(root.LeftChild);frontOrderMirror(root.RightChild);}
非递归方法:
static void frontOrderMirrorNoRecur(Node root){Stack<Node> cache = new Stack<Node>();Node p = root;while (cache.Count != 0 || p != null){while (p != null){Node q;q = p.LeftChild;p.LeftChild = p.RightChild;p.RightChild = q;cache.Push(p);p = p.LeftChild;}if (cache.Count != 0){p = cache.Pop();p = p.RightChild;}}}
6,层序遍历二叉树
static int GetDeep(Node root) {if (root == null) return 0;int leftDeep = GetDeep(root.LeftChild);int rightDeep = GetDeep(root.RightChild);int maxDeep = (leftDeep > rightDeep ? leftDeep : rightDeep)+1;return maxDeep;}static void OneLevelTravel(Node root,int level){if (root == null) return;if(level == 1)Console.WriteLine("{0}", root.Value);OneLevelTravel(root.LeftChild, level-1);OneLevelTravel(root.RightChild, level-1); }static void AllOneLevelTravel(Node root, int level){for (int i = 1; i <= level; i++) {OneLevelTravel(root, i);}}
7, 递归和非递归俩种方法实现二叉树的后序遍历。
static void FollowOrderNoRecur(Node root){Stack<Node> cache = new Stack<Node>();Node p = root;while (cache.Count != 0 || p != null){while (p != null){cache.Push(p);p = p.LeftChild;}if (cache.Count != 0){p = cache.Pop();if (p.Flag == 1){cache.Push(p);p.Flag = 0;p = p.RightChild; }else{Console.WriteLine("{0}", p.Value);p = null;} } }}
8, 输入一棵二元树的根结点,求该树的深度。
从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
9, 二叉树两个结点的最低共同父结点(树)
class Node{private int flag = 1;public Node LeftChild { get; set;}public Node RightChild { get; set;}public char Value{get;set;}public int Flag { get{return flag ;} set{flag = value;} }}class LinkNode{public LinkNode Next{ get; set; }public char Value { get; set; }}static Node FindCommonParent(Node node1, Node node2 ,Node root){if (node1 == null || node2 == null || root == null) return null;LinkNode head1 = new LinkNode();LinkNode head2 = new LinkNode();FindPath(root, node1, head1);FindPath(root, node2, head2);LinkNode common = FindFirstCommondNode(head1, head2);if(common != null){return new Node { Value = common.Value };}return null;}static LinkNode FindFirstCommondNode(LinkNode head1, LinkNode head2){int count1 = 0; int count2 = 0;LinkNode p = head1;while (p != null) {count1++;p = p.Next;}p = head2;while (p != null){count2++;p = p.Next;}int distance = Math.Abs(count1-count2);if (count1 > count2){for (int i = 0; i < distance; i++){head1 = head1.Next;}}else {for (int i = 0; i < distance; i++){head2 = head2.Next;} }while (head1 != null && head2 != null){if (head1.Value == head2.Value){return head1;}head1 = head1.Next;head2 = head2.Next;}return null;}static LinkNode FindPath(Node root, Node target, LinkNode linkHead){if (root == null) return null;if (target.Value == root.Value) {linkHead.Value = root.Value;return linkHead; }LinkNode p =null;p = FindPath(root.LeftChild, target, linkHead);if (p == null)p = FindPath(root.RightChild, target, linkHead);if (p != null) {p.Next = new LinkNode {Value = root.Value };return p.Next;}return null;}
10, 怎样编写一个程序,把一个有序整数数组放到二叉树中?
11,判断一个数字序列是BST后序遍历的结果
12,给一个二叉树,每个节点都是正或负整数,如何找到一个子树,它所有节点的和最大?
13,有一个排序二叉树,数据类型是int型,如何找出中间大的元素
14,二叉树查找不严格小于一个值的最大值(返回节点)。
15,中序遍历二叉树,结果为ABCDEFGH,后序遍历结果为ABEDCHGF,那么前序遍历结果为
static void CreatTreeByMidandFollow(Node root, int MidStartIndex, int MidEndIndex, int FollowStartIndex,int FollowEndIndex,char[] mid, char[] follow) {int rootInMidIndex = 0;for (int i = MidStartIndex; i <= MidEndIndex; i++){if (mid[i] == follow[FollowEndIndex]){rootInMidIndex = i;break;}}root.Value = follow[FollowEndIndex];int rightLength = MidEndIndex - rootInMidIndex;int leftLength = MidEndIndex - MidStartIndex - rightLength;if (leftLength > 0) {int leftRootIndex = FollowStartIndex + leftLength-1;root.LeftChild = new Node();CreatTreeByMidandFollow(root.LeftChild, MidStartIndex, rootInMidIndex - 1, FollowStartIndex, leftRootIndex, mid, follow);}if (rightLength > 0) {int rightRootIndex = FollowEndIndex - 1;root.RightChild = new Node();CreatTreeByMidandFollow(root.RightChild, rootInMidIndex+1, MidEndIndex, FollowStartIndex + leftLength , rightRootIndex, mid, follow);}}
16,对于一个多叉树,设计TreeNode节点和函数,返回先序遍历情况下的下一个节点。
17,将一个普通的二叉树转换为二叉排序树
18,给定一棵树根节点,每个节点里面的值都不相同,查找iKEY的节点,并使用一个给定的节点将查找到的节点替换掉。节点内有两个孩子节点和一个父节点。
19,如何删除一个搜索二叉树的结点
20, 四种遍历,递归非递归
23,实现完整二叉查找树(Init,Search,Insert,Update,Delete,Length)
转载于:https://www.cnblogs.com/msfte/archive/2012/12/18/2823362.html
算法——树(10%)相关推荐
- LCA 朴素算法+树差分倍增+Tarjan算法 三种算法实现c++代码实现
哔哩哔哩up视频:https://www.bilibili.com/video/BV1nE411L7rz?t=379 转载:http 文章目录 树差分 & 倍增LCA Tarjan 朴素算法 ...
- 2021首期Nature封面:牛津大学ML算法实现10万高压非晶硅原子的模拟 | AI日报
2021首期Nature封面:牛津大学ML算法实现10万高压非晶硅原子的模拟 为了对一般无序结构材料有更深的理解,人们广泛研究了非晶硅在高压条件下的富相行为.然而在和原子打交道的层面上,人们一直需要借 ...
- 数据结构与算法:树 二叉树入门(一)
Tips: 采用java语言,关注博主,底部附有完整代码 工具:IDEA 本系列介绍的是数据结构: 树 这是第1篇目前计划一共有12篇: 二叉树入门 本篇 顺序二叉树 线索化二叉树 堆排序 赫夫曼树( ...
- 基于蚁群算法的10个城市TSP问题的最短路径研究(附源码及讲解步骤)
基于蚁群算法的10个城市TSP问题的最短路径研究 欢迎关注:天际使徒的个人博客 1 蚁群算法 1.1 蚁群算法的流程步骤 这里以TSP问题为例,算法设计的流程如下: 步骤1:对相关参数进行初始化,包括 ...
- 银行卡号校验位的LUHN算法模10“隔位2倍加”校验数的公式
银联卡卡号由三部分组成:发卡机构标识码(bin).发卡机构自定义位.校验码.其中,卡号左起前六位是发卡机构标识代码(BIN),由6位数字组成.BIN号由中国银联复制分配.确认和管理:卡号第七位起事发卡 ...
- Morris遍历算法 树的中序遍历
Morris遍历算法 树的中序遍历 树的中序遍历 一.普通方法 1.递归实现 2.栈实现 二.Morris遍历 1.算法 2.代码 总结 树的中序遍历 对于当前结点,先输出它的左孩子,然后输出该结点, ...
- 算法6.10 迪杰斯特拉算法
算法6.10 迪杰斯特拉算法 代码实现 //算法6.10 迪杰斯特拉算法 #pragma once #include <iostream>using namespace std;//图的邻 ...
- c语言:利用冒泡算法对10个数字进行从小到大的排序
利用冒泡算法对10个数字进行从小到大的排序 #include <stdio.h> int main() {int a[10];int i,j,t;printf("请输入10个数字 ...
- Java数据结构与算法——树(基本概念,很重要)
声明:码字不易,转载请注明出处,欢迎文章下方讨论交流. 有网友私信我,期待我的下一篇数据结构.非常荣幸文章被认可,也非常感谢你们的监督. 前言:Java数据结构与算法专题会不定时更新,欢迎各位读者监督 ...
最新文章
- travis-ci如何配置android
- linux awk语法格式,Awk是什么?一文带运维小白快速掌握Linux Awk用法
- 8.文本处理(编码解码/文件操作)
- shiro登陆流程源码详解
- 一个关于winform多线程的教程(pdf)
- .NET开源5年了,这些宝藏你还没get?
- 获奖者:舒继武,男,清华大学计算机系教授、博士生导师,教育部长江学者特聘教授,国家杰出青年基金获得者,《大数据》杂志编委。...
- android基础知识学习(1) TextView属性大全+单行显示长文本
- 系统测试与端到端测试:哪一个更适合选择?
- JavaScript 中的相等检测
- mysql配置文件my.cnf详解
- Extjs中引入JSP页面
- python生成excel模板_使用python创建excel表格 --- XlsxWriter模板详解
- D触发器实现JK触发器
- 前端JS-页面延迟刷新
- vue中导出excel表格(支持导出图片)
- html 怎么检测ie浏览器的最高版本,检测是否为IE浏览器及IE浏览器的版本
- 关于屏幕分辨率适配的教程
- JS padStart()方法和padEnd()方法(ES6新增方法)
- JavaScript时间的处理