Given a binary tree and two given nodes, find the distance between the two nodes.
Do not consider it the maximum distance between the nodes of a binary tree. The question is the distance between two nodes.
Questions can be found in several situations
- Two nodes are distributed on the left or right subtree of the root node.
- A node is distributed in the left subtree of the root node, and a node is distributed in the right subtree of the root node.
- The two nodes are sibling nodes.
- One node is the ancestor node of another node
The solution to this question is
The hierarchy Traversal method is used to obtain the height of each node. The height of the Left subtree of the root node is represented by a positive number, and the height of the right subtree of the root node is represented by a negative number.
In this way, when two nodes are distributed in: one node is distributed in the left subtree of the root node, and one node is distributed in the right subtree of the root node, only the absolute value of the height difference between the two nodes is required.
If one node is the ancestor node of another node, you only need a high node to locate a small height node along the ancestor node. If the nodes reach the same height and are not the same, it indicates a sibling node relationship.
#include <iostream>#include <vector>#include <queue>#include <stack>#include <string>#include <algorithm>using namespace std;struct TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int val_ = 0):val(val_),left(NULL),right(NULL){}};struct TreeHeightNode{ int height; TreeNode* node; TreeHeightNode* parent; TreeHeightNode(TreeNode* node_ = NULL,int height_ = 0,TreeHeightNode* parent_ =NULL): node(node_), height(height_),parent(parent_){}};int getDistanceBetweenNode(TreeNode* root,TreeNode* a, TreeNode* b){ if(root == NULL ) return -1; queue<TreeHeightNode *> que; que.push(new TreeHeightNode(root,0)); bool flagA = false, flagB = false; // int heightA = 0, heightB = 0; TreeHeightNode* heightA =NULL, *heightB = NULL; while(!que.empty()){ TreeHeightNode* tmp = que.front(); que.pop(); TreeNode *node = tmp->node; if(node == a) {flagA = true;heightA = tmp;} if(node == b) {flagB = true;heightB = tmp;} if(flagA && flagB) break; if(node->left){ if(node->val == 0) que.push(new TreeHeightNode(node->left,1,tmp)); else if(node->val > 0) que.push(new TreeHeightNode(node->left,tmp->height+1,tmp)); else if(node->val < 0) que.push(new TreeHeightNode(node->left,tmp->height-1,tmp)); } if(node->right){ if(node->val == 0) que.push(new TreeHeightNode(node->right,-1,tmp)); else if(node->val > 0) que.push(new TreeHeightNode(node->right,tmp->height+1,tmp)); else if(node->val < 0) que.push(new TreeHeightNode(node->right,tmp->height-1,tmp)); } } if(!flagA || !flagB) return -1; else{ int ha = heightA->height, hb =heightB->height; if(ha*hb <=0) return ha-hb; else{ if((ha >= hb && hb > 0) || (ha < hb && hb < 0)){ int cnt = ha-hb; while(cnt-->0){ heightA=heightA->parent; } if(heightA == heightB) return ha-hb; else return ha-hb+2; }else{ int cnt = hb - ha; while(cnt-- > 0){ heightB=heightB->parent; } if(heightB == heightA) return hb-ha; else return hb-ha+2; } } }}int main(){ TreeNode *root = new TreeNode(1); TreeNode *left = new TreeNode(2); TreeNode *right = new TreeNode(3); root->left = left; root->right = right; TreeNode *left1 = new TreeNode(4); TreeNode *right1 = new TreeNode(5); left->left = left1; left->right = right1; TreeNode *left2 = new TreeNode(4); TreeNode *right2 = new TreeNode(5); left1->left = left2; left1->right = right2; cout<<getDistanceBetweenNode(root,left2,right)<<endl; return 0;}
View code
In fact, finding the distance between two nodes is to find the common ancestor nodes of the two nodes, so the other method is to find the least common ancestor node of O.
Then