Problem descriptionRT. If the two-fork tree is viewed as a graph, the connection of the parent-child node is considered bidirectional, and the distance is defined as the number of two nodes.Solution IdeasThe height of the left subtree of the root node and the height of the right subtree are added.Programpublic class Maxdistanceintree {public int getmaxdistanceintree (TreeNode root) {if (root = null) {return 0;} int leftheight = Getheightoftree (root.l
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/AD/wKiom1esENLzdGt4AACY_uRxu7s509.png "title=" capture. PNG "alt=" Wkiom1esenlzdgt4aacy_urxu7s509.png "/>Data structure of #include This article from "Zero Egg" blog, declined reprint!Find the distance between the maximum and minimal leaf nodes in a binary tree
There are two situations: either the depth of the tree (for example, the linked list), or the depth of the two Subtrees and the addition of 2. The final result is either the maximum distance from a subtree, or the depth and addition of 2 from the two subtree.
The specific procedure is as follows:
#include
References:
Programming beauty 3.8 p241
Http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.htm
Title DescriptionIf we look at the two-fork tree as a graph, and the connection between the parent and child nodes is two-way, we'll define the "distance" as a variable between the two nodes.Write a program to find the distance between the two nodes farthest apart in a binary tree.Input requirementsThe first line of the input contains a separate number T, indicating the number of test sequences;Each of the
Seeing this question, I immediately thought of finding the depth of the binary tree (by finding the depth of the left and right Subtrees respectively, and then merging (taking the maximum value and adding 1) thus, the depth of the root node is obtained (in fact, the idea of sub-governance ))
The Code is as follows:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->int Depth(Node *p){int l_d , r_d;if(p
meaningless ....
The solution to the original question will be discussed later.Algorithm:
The question is to find the longest path in a tree.
For node T, the longest path of the tree with it as the root must be one of the following threeMaximum Value:
Longstpath of the Left subtree of ① t
② The longstpath of the right subtree of T
③ Depth of the Left subtree of T + depth of the right subtree of T + 2
-- Conclusion 1
Therefore, each node has two important attributes:Depth② For the tree with the
Problem Solving Ideas:To find the two endpoints of the tree's diameter, the farthest distance from each node to the other points on the tree must be the longest distance between the two endpoints.#include Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. HDU 2196 computer (maximum
Topic:Find the farthest distance of the two nodes of the binary tree.The binary tree is defined as follows:Class Treenode{public: int val; Treenode* left; treenode* right; TreeNode (int x): Val (x), left (null), right (null) {}};Ideas:Traverse each node to find the longest path at the root of the current node, and then find the maximum value in all the longest paths.Code:Code Listing 1:Class Nod
Problem:
The N * n table contains natural numbers. Starting from any location, the numbers in the table can be moved to the four adjacent domains (provided that the numbers in the direction of movement must be smaller than the current number ), move one distance and add one. What is the maximum moving distance?
Analysis:
The problem can be considered as a dyna
#include Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Programming beauty finding the maximum distance between nodes in a binary tree
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.