PHP calculates the distance between two longest Binary Trees

Source: Internet
Author: User
1 <? PHP 2 # search for the distance between the two nodes with the longest binary tree, that is, the number of edges of the two nodes with the highest number of edges. 3 # solution: the two nodes with the longest distance have the following three conditions: 4 #1. if one of the two nodes is in the left subtree and the other is in the right subtree, the longest distance is the addition of the depth of the left and right subtree 5 #2. if both nodes are in the left subtree, the longest distance is the depth of the left and right subtree of the Left subtree with the left subtree as the root node. 6 #3. both nodes are located in the right subtree, the longest distance is to add the depth of the left and right subtree of the right subtree with the right subtree as the root node. 7 8 class node {9 Public $ DATA = NULL; 10 public $ left = NULL; 11 Public $ right = NULL; 12 public $ parent = NULL; 13} 14 15 function get_max_long ($ root, & $ depth) {16 # combined with binary tree deep computing 17 if ($ root = NULL) {1 8 $ depth = 0; 19 Return 0; 20} 21 # Calculate the longest distance between left and right subtree, and calculate the depth of left and right subtree 22 $ left_max = get_max_long ($ root-> left, $ LD); 23 $ right_max = get_max_long ($ root-> right, $ rd); 24 $ depth = max ($ lD, $ rd) + 1; 25 26 // echo "{$ LD} | {$ RD} | {$ left_max} | {$ right_max} <br>"; 27 return max ($ LD + $ Rd, max ($ left_max, $ right_max); 28} 29 30 # Binary Tree depth algorithm 31 #32 function get_depth ($ root) {33 If (! $ Root) {34 return 0; 35} 36 37 $ LD = get_depth ($ root-> left) + 1; 38 $ RD = get_depth ($ root-> right) + 1; 39 40 return max ($ lD, $ rd); 41} 42 43 $ root = new node (); 44 $ n1 = new node (); 45 $ n2 = new node (); 46 $ N11 = new node (); 47 $ n12 = new node (); 48 $ N13 = new node (); 49 $ N14 = new node (); 50 $ N15 = new node (); 51 $ n21 = new node (); 52 53 $ root-> left = $ N1; 54 $ root-> right = $ N2; 55 $ N1-> left = $ N11; 56 $ N1-> right = $ N12; 57 $ N11-> left = $ N13; 58 $ n12-> right = $ N14; 59 $ N13-> left = $ N15; 60 $ N2-> right = $ n21; 61 62 $ max = get_max_long ($ root, $ depth); 63 echo $ Max; 64?>

4 10 3 1 7 11 8 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.