博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树求最短路径,高度,最大宽度
阅读量:6120 次
发布时间:2019-06-21

本文共 5189 字,大约阅读时间需要 17 分钟。

hot3.png

package com.weshare.eel.task.utils;import com.jayway.jsonpath.internal.function.numeric.Max;import java.util.ArrayDeque;import java.util.Queue;import java.util.Stack;public class FindShortestPath2 {    private static boolean isLeafFind = false;    private static String path1;    public static void main(String[] args) {        TreeNode node1 = new TreeNode(1);        TreeNode node2 = new TreeNode(2);        TreeNode node3 = new TreeNode(3);        TreeNode node4 = new TreeNode(4);        TreeNode node5 = new TreeNode(5);        TreeNode node6 = new TreeNode(6);        TreeNode node7 = new TreeNode(7);        TreeNode node8 = new TreeNode(8);        node5.setLeft(node2);        node5.setRight(node7);        node7.setLeft(node6);        node7.setRight(node8);        node2.setLeft(node1);        node2.setRight(node3);        node3.setRight(node4);        //findPathOfTwoNode(node5, node3, node6);        Stack
path = new Stack
(); findPath(node5, path); System.out.println(path); System.out.println(findHeight(node5)); System.out.println(findWidth(node5)); } private static int findWidth(TreeNode root) { if (root == null) { return 0; } int maxWidth = 1; Queue
path = new ArrayDeque<>(); path.add(root); while (true) { int len = path.size(); if (len == 0) { break; } while (len > 0) { TreeNode node = path.poll(); len--; if (node.getLeft() != null) { path.add(node.getLeft()); } if (node.getRight() != null) { path.add(node.getRight()); } } maxWidth = Math.max(maxWidth, path.size()); } return maxWidth; } public static int findHeight(TreeNode root) { if (root == null) { return 0; } int left = findHeight(root.getLeft()); int right = findHeight(root.getRight()); return 1 + Math.max(left, right); } public static void findPath(TreeNode root, Stack
path) { if (root == null) { return; } path.push(root.getValue()); if (root.getLeft() != null) { findPath(root.getLeft(), path); } //查询右子树 if (root.getRight() != null) { findPath(root.getRight(), path); } } public static String findPath(TreeNode root, Stack
path, TreeNode nodeToFind) { if (root == null) { return null; } path.push(root.getValue()); if (!isLeafFind && nodeToFind == root) { isLeafFind = true; path1 = printPath(path); return path1; } if (!isLeafFind && root.getLeft() != null) { findPath(root.getLeft(), path, nodeToFind); } //查询右子树 if (!isLeafFind && root.getRight() != null) { findPath(root.getRight(), path, nodeToFind); } //如果没找到则弹栈 if (!isLeafFind) { path.pop(); } return path1 == null ? null : path1; } public static String printPath(Stack
path) { int len = path.size(); String s = "" + path.pop(); for (int i = 1; i < len; i++) { if (path.peek() != null) { s += "->" + path.pop(); } } System.out.println(s); return s; } public static TreeNode findCommonParent(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) { return root; } TreeNode left = findCommonParent(root.getLeft(), p, q); TreeNode right = findCommonParent(root.getRight(), p, q); if (left != null && right != null) { return root; } return left == null ? right : left; } private static void findPathOfTwoNode(TreeNode root, TreeNode p, TreeNode q) { Stack
path1 = new Stack
(); Stack
path2 = new Stack
(); //寻找两个路径的交点,即最小公共祖先 TreeNode lca = findCommonParent(root, p, q); //得到p节点的路径 System.out.println("最小公共祖先节点" + lca.getValue() + "和节点" + p.getValue() + "之间的路径"); String s1 = findPath(lca, path1, p); isLeafFind = false;//全局变量复位 //得到q节点的路径 System.out.println("最小公共祖先节点" + lca.getValue() + "和节点" + q.getValue() + "之间的路径"); String s2 = findPath(lca, path2, q); isLeafFind = false;//全局变量复位 //合并两条路径去掉重复的最小公共祖先 String[] split = s2.split("->"); String s3 = s1 + "->" + split[0]; for (int i = 1; i < split.length; i++) { if (Integer.parseInt(split[i]) != lca.getValue()) { s3 += "->" + split[i]; } } System.out.println("归并后的路径为:" + s3); }}

 

package com.weshare.eel.task.utils;public class TreeNode {    private int value;    private TreeNode left;    private TreeNode right;    public TreeNode(int value) {        this.value = value;    }    public int getValue() {        return value;    }    public void setValue(int value) {        this.value = value;    }    public TreeNode getLeft() {        return left;    }    public void setLeft(TreeNode left) {        this.left = left;    }    public TreeNode getRight() {        return right;    }    public void setRight(TreeNode right) {        this.right = right;    }}

转载于:https://my.oschina.net/u/3824443/blog/2874440

你可能感兴趣的文章
Retrofit 源码剖析-深入
查看>>
企业级负载平衡简介(转)
查看>>
ICCV2017 论文浏览记录
查看>>
科技巨头的交通争夺战
查看>>
当中兴安卓手机遇上农行音频通用K宝 -- 卡在“正在通讯”,一直加载中
查看>>
Shell基础之-正则表达式
查看>>
JavaScript异步之Generator、async、await
查看>>
讲讲吸顶效果与react-sticky
查看>>
c++面向对象的一些问题1 0
查看>>
直播视频流技术名词
查看>>
网易跟贴这么火,背后的某个力量不可忽视
查看>>
企业级java springboot b2bc商城系统开源源码二次开发-hystrix参数详解(八)
查看>>
java B2B2C 多租户电子商城系统- 整合企业架构的技术点
查看>>
IOC —— AOP
查看>>
比特币现金将出新招,推动比特币现金使用
查看>>
数据库的这些性能优化,你做了吗?
查看>>
某大型网站迁移总结(完结)
查看>>
mysql的innodb中事务日志(redo log)ib_logfile
查看>>
部署SSL证书后,网页内容造成页面错误提示的处理办法
查看>>
MS SQLSERVER通用存储过程分页
查看>>