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<Integer> path = new Stack<Integer>(); 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<TreeNode> 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<Integer> 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<Integer> 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<Integer> 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<Integer> path1 = new Stack<Integer>(); Stack<Integer> path2 = new Stack<Integer>(); //尋找兩個路徑的交點,即最小公共祖先 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; } }