在二叉树中找到两个节点的最近公共祖先

来源:互联网 发布:软件展示html模版 编辑:程序博客网 时间:2024/05/15 23:45


import java.util.*;//在二叉树中找到两个节点的最近公共祖先public class GetLowestTree{//二叉树节点的定义public static class Node{public int value;public Node left;public Node right;public Node(int data){this.value=data;}}//找到两个节点的最近公共祖先public static Node GetLowestAncestor(Node head,Node n1,Node n2){   //递归的出口if(head==null||n1==head||n2==head){return head;}         //递归调用Node left=GetLowestAncestor(head.left,n1,n2);  //左子树Node right=GetLowestAncestor(head.right,n1,n2); //右子树if(left!=null&&right!=null){return head;}return left!=null?left:right;}//***************************************************   // 直观打印二叉树public static void printTree(Node head) {System.out.println("Binary Tree:");printInOrder(head, 0, "H", 17);System.out.println();}public static void printInOrder(Node head, int height, String to, int len) {if (head == null) {return;}printInOrder(head.right, height + 1, "v", len);String val = to + head.value + to;int lenM = val.length();int lenL = (len - lenM) / 2;int lenR = len - lenM - lenL;val = getSpace(lenL) + val + getSpace(lenR);System.out.println(getSpace(height * len) + val);printInOrder(head.left, height + 1, "^", len);}public static String getSpace(int num) {String space = " ";StringBuffer buf = new StringBuffer("");for (int i = 0; i < num; i++) {buf.append(space);}return buf.toString();}//***************************************************  // 进阶问题--方法一(构造hash表)public static class Record1 {private HashMap<Node, Node> map;public Record1(Node head) {map = new HashMap<Node, Node>();if (head != null) {map.put(head, null);}setMap(head);}private void setMap(Node head) {if (head == null) {return;}if (head.left != null) {map.put(head.left, head);}if (head.right != null) {map.put(head.right, head);}setMap(head.left);setMap(head.right);}public Node query(Node o1, Node o2) {HashSet<Node> path = new HashSet<Node>();while (map.containsKey(o1)) {path.add(o1);o1 = map.get(o1);}while (!path.contains(o2)) {o2 = map.get(o2);}return o2;}}// 进阶问题--方法二public static class Record2 {private HashMap<Node, HashMap<Node, Node>> map;public Record2(Node head) {map = new HashMap<Node, HashMap<Node, Node>>();initMap(head);setMap(head);}private void initMap(Node head) {if (head == null) {return;}map.put(head, new HashMap<Node, Node>());initMap(head.left);initMap(head.right);}private void setMap(Node head) {if (head == null) {return;}headRecord(head.left, head);headRecord(head.right, head);subRecord(head);setMap(head.left);setMap(head.right);}private void headRecord(Node n, Node h) {if (n == null) {return;}map.get(n).put(h, h);headRecord(n.left, h);headRecord(n.right, h);}private void subRecord(Node head) {if (head == null) {return;}preLeft(head.left, head.right, head);subRecord(head.left);subRecord(head.right);}private void preLeft(Node l, Node r, Node h) {if (l == null) {return;}preRight(l, r, h);preLeft(l.left, r, h);preLeft(l.right, r, h);}private void preRight(Node l, Node r, Node h) {if (r == null) {return;}map.get(l).put(r, h);preRight(l, r.left, h);preRight(l, r.right, h);}public Node query(Node o1, Node o2) {if (o1 == o2) {return o1;}if (map.containsKey(o1)) {return map.get(o1).get(o2);}if (map.containsKey(o2)) {return map.get(o2).get(o1);}return null;}}public static void main(String[]args){//System.out.println("Hello");Node head=new Node(1);head.left=new Node(2);head.right=new Node(3);head.left.left=new Node(4);head.left.right=new Node(5);head.right.left=new Node(6);head.right.right=new Node(7);head.right.right.left=new Node(8);            printTree(head);        Node node=GetLowestAncestor(head,head.left,head.right);System.out.println(node.value);        Node o1 = head.left.right;Node o2 = head.right.left;// 生成map后方便多次查询--进阶问题Record1 record1 = new Record1(head);Record2 record2 = new Record2(head);System.out.println("o1 : " + o1.value);System.out.println("o2 : " + o2.value);System.out.println("ancestor : " + record1.query(o1, o2).value);System.out.println("ancestor : " + record2.query(o1, o2).value);o1 = head.left.left;o2 = head.left;System.out.println("o1 : " + o1.value);System.out.println("o2 : " + o2.value);System.out.println("ancestor : " + record1.query(o1, o2).value);System.out.println("ancestor : " + record2.query(o1, o2).value);o1 = head.right.left;o2 = head.right.right.left;System.out.println("o1 : " + o1.value);System.out.println("o2 : " + o2.value);System.out.println("ancestor : " + record1.query(o1, o2).value);System.out.println("ancestor : " + record2.query(o1, o2).value);}}


阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝买的东西电话号码留错了怎么办 平板电脑没电关机没保存文件怎么办 恢复出厂设置需要谷歌账号怎么办 华为手机云端里照片删除了怎么办 客户退货卖家一直没收到货怎么办 在淘宝买到假货投诉不管用怎么办 差评不接电话不回旺旺不要钱怎么办 饿了么同行恶意差评怎么办 苹果手机更新后淘宝用不了怎么办 淘宝网快递丢件了买家怎么办 评价后忘了截图五星好评怎么办 在淘宝被骗了好评返现怎么办 苹果4s微信版本过低怎么办 微信版本太低无法登录怎么办 苹果4微信版本低登录不了怎么办 安卓手机微信版本低登录不了怎么办 苹果微信版本低登录不了怎么办 安装包与当前版本不兼容怎么办 游戏与苹果手机系统不兼容怎么办 闲鱼买家申请退款不退货怎么办 在淘宝买的战地1登录不了怎么办 支付宝转账到注销的账号怎么办 支付宝用户支付密码被锁定怎么办 淘宝提现需要的手机宝令怎么办 手机换号码了支付宝账号怎么办 卖家收到货迟迟不给退款怎么办 闲鱼东西已发出买家申请退款怎么办 如果东西在路上买家要退款怎么办 卖家收到退的货后不退款怎么办 扣48分淘宝被限制登录怎么办 淘宝网申请退款卖家不理怎么办 高考生如果把登录密码忘记了怎么办 普高考生登录密码忘记了怎么办 淘宝买的流量卡然后网卡怎么办? 忘记了qq号绑定了手机怎么办 微信绑定qq号忘记了怎么办 扣扣密码忘了手机号换了怎么办 如何更改微信登录密码忘记了怎么办 阿里卖家不肯同意退货退款怎么办 新买的号码被绑定微信怎么办 如果换手机原来旧手机支付宝怎么办