首页 > 代码库 > 求一个二叉树中距离最远的两个节点
求一个二叉树中距离最远的两个节点
/*求二叉树中距离最远的两个点 * 基本思路: * 递归计算两棵树的最大高度,设置一个全局变量,距离最远的两个节点element * 其中:在计算左子支,直接刷新上述全局变量,在计算右边子支时,设置两个临时Node变量,变量里的element用于 * 保存右边子支的两个最远距离。根据比较两个距离的大小、其父节点所在的树三个的大小,来重新刷新全局变量。 * 一个Trick~:在计算子支的最远距离的时候,因为要和其父节点所在的树比较大小,保存子支的最大距离的点数。 */public class MaxLenTree { public Node root; public int len = 0; public class Node { char element; int hight; Node left; Node right; public Node(char element, int hight, Node left, Node right) { this.element = element; this.hight = hight; this.left = left; this.right = right; } Node() { } } MaxLenTree() {/* Node e = new Node(‘e‘, 0, null, null); Node d = new Node(‘d‘, 0, e, null); Node f = new Node(‘f‘, 0, null, null); Node c = new Node(‘c‘, 0, d, f); Node b = new Node(‘b‘, 0, c, null); Node a = new Node(‘A‘, 0, b, null);*/ Node m = new Node(‘m‘, 0, null, null); Node n = new Node(‘n‘, 0, null, null); Node k = new Node(‘k‘, 0, m, n); Node l = new Node(‘l‘, 0, null, null); Node i = new Node(‘i‘, 0, null, null); Node j = new Node(‘j‘, 0, k, l); Node q = new Node(‘q‘, 0, null, null); Node p = new Node(‘P‘, 0, q, null); Node h = new Node(‘h‘, 0, p, null); Node d = new Node(‘d‘, 0, h, null); Node e = new Node(‘e‘, 0, i, j); Node b = new Node(‘B‘, 0, d, e); Node f = new Node(‘f‘, 0, null, null); Node g = new Node(‘g‘, 0, null, null); Node c = new Node(‘c‘, 0, f, g); Node a = new Node(‘A‘, 0, b, c); root = a; } public static void main(String[] args) { MaxLenTree mTree = new MaxLenTree(); mTree.calculateHight(); mTree.run(); } public void run() { Node node = new Node(); System.out.println(findMaxLen(this.root, node)); System.out.println("maxNode left.element---" + maxNodeleft.element); System.out.println("maxNoderight.element---" + maxNoderight.element); } public Node maxNodeleft = new Node(); public Node maxNoderight = new Node(); public int findMaxLen(Node root, Node LongestChild) { System.out.println(LongestChild.element); if (root.left == null && root.right == null) { LongestChild.element = root.element; maxNodeleft.element = root.element; maxNoderight.element = root.element; return 0; } Node leftLongestChild = new Node(); Node rightLogestChild = new Node(); if (root.left == null || root.right == null) { if (root.right == null) { int a = findMaxLen(root.left, leftLongestChild); System.out.println(leftLongestChild.element); if (hight(root) > a) { LongestChild.element = leftLongestChild.element; this.maxNodeleft.element = leftLongestChild.element; this.maxNoderight.element = root.element; return hight(root); } else { LongestChild.element = leftLongestChild.element; return a; } } else { int a = findMaxLen(root.right, rightLogestChild); if (hight(root) > a) { LongestChild.element = rightLogestChild.element; this.maxNodeleft.element = root.element; this.maxNoderight.element = rightLogestChild.element; return hight(root); } else { LongestChild.element = rightLogestChild.element; return a; } } } int a = findMaxLen(root.left, leftLongestChild); Node rightNodetempLeft = new Node(); rightNodetempLeft.element = maxNodeleft.element; Node rightNodetempRight = new Node(); rightNodetempRight.element = maxNoderight.element; int b = findMaxLen(root.right, rightLogestChild); int c = Math.max(a, b); if (a > b) { maxNodeleft.element = rightNodetempLeft.element; maxNoderight.element = rightNodetempRight.element; } if (c == b) { LongestChild.element = rightLogestChild.element; } else { LongestChild.element = leftLongestChild.element; } int d = hight(root.left) + hight(root.right) + 2; if (c < d) { maxNodeleft.element = leftLongestChild.element; maxNoderight.element = rightLogestChild.element; } System.out.println(LongestChild.element); return Math.max(c, d); } public int hight(Node root) { if (root == null) { return -1; } return root.hight; } public void showTree(Node root) { if (root == null) { return; } System.out.println(root.element + "<----->" + root.hight); showTree(root.left); showTree(root.right); } public void calculateHight() { calculateHight(this.root); } public int calculateHight(Node root) { if (root == null) { return -1; } if (root.right == null && root.left == null) { root.hight = 0; return 0; } int temp = Math.max(calculateHight(root.left), calculateHight(root.right)) + 1; root.hight = temp; return temp; }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。