当前位置:网站首页>Given a node of a binary tree, return the successor node of the node
Given a node of a binary tree, return the successor node of the node
2022-06-23 06:12:00 【Bright morning light】
1、 subject
The binary tree structure is defined as follows :
class TreeNode {
public:
V value;
TreeNode *left;
TreeNode *right;
TreeNode *parent;
};
Given a node in a binary tree , Returns the successor node of this node .
2、 analysis
The subsequent nodes : In the sequence traversal The next node of the current node in the sequence .
Classic practice : Given the root node , Middle order traversal generates a sequence , Find the next node of a given node in this sequence , Time complexity O ( N ) O(N) O(N).
But through the binary tree structure , Can know the left child of the current node 、 The right child and his father , So there is a O ( k ) O(k) O(k) ( among k k k by The true distance from the current node to the successor node ) The time complexity algorithm can find the successor nodes of a given node .
This requires a structural analysis of the relationship between a node and its successor nodes .
There are two situations :
- A given X node There is a right tree , Because the subsequent nodes are in the middle order traversal sequence X Next , So there's no doubt about it ,X The next step in the middle order traversal sequence must be X The leftmost child on the right tree ;
- A given X node No right tree ,X Keep looking up , Find that one node is another node P The left child , that P Namely X Successor node , The essence is to find X Which node is the rightmost node in the left tree .
3、 Realization
C++ edition
/************************************************************************* > File Name: 032. Returns the successor node of a given node .cpp > Author: Maureen > Mail: [email protected] > Created Time: 3、 ... and 6/22 12:15:48 2022 ************************************************************************/
#include <iostream>
using namespace std;
class TreeNode {
public:
int value;
TreeNode *left;
TreeNode *right;
TreeNode *parent;
TreeNode(int v) : value(v) {
}
};
// Find the leftmost child in the right tree
TreeNode *getLeftMost(TreeNode *node) {
if (node == nullptr)
return nullptr;
while (node->left != nullptr) {
node = node->left;
}
return node;
}
TreeNode *getSuccessorTreeNode(TreeNode *node) {
if (node == nullptr) return node;
if (node->right != nullptr) {
// Right tree exists
return getLeftMost(node->right);
} else {
// There is no right tree
TreeNode *parent = node->parent;
// The current node is the right child of its parent node
while (parent != nullptr && parent->right == node) {
node = parent;
parent = node->parent;
}
return parent;
}
}
int main() {
TreeNode *root = new TreeNode(6);
root->parent = nullptr;
root->left = new TreeNode(3);
root->left->parent = root;
root->left->left = new TreeNode(1);
root->left->left->parent = root->left;
root->left->left->right = new TreeNode(2);
root->left->left->right->parent = root->left->left;
root->left->right = new TreeNode(4);
root->left->right->parent = root->left;
root->left->right->right = new TreeNode(5);
root->left->right->right->parent = root->left->right;
root->right = new TreeNode(9);
root->right->parent = root;
root->right->left = new TreeNode(8);
root->right->left->parent = root->right;
root->right->left->left = new TreeNode(7);
root->right->left->left->parent = root->right->left;
root->right->right = new TreeNode(10);
root->right->right->parent = root->right;
TreeNode *test = root->left->left;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->left->left->right;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->left;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->left->right;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->left->right->right;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->right->left->left;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->right->left;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->right;
cout << test->value << " next: " << getSuccessorTreeNode(test)->value << endl;
test = root->right->right; // 10's next is null
cout << test->value << " next: " << getSuccessorTreeNode(test) << endl;
return 0;
}
Java edition
public class SuccessorNode {
public static class Node {
public int value;
public Node left;
public Node right;
public Node parent;
public Node(int data) {
this.value = data;
}
}
public static Node getSuccessorNode(Node node) {
if (node == null) {
return node;
}
if (node.right != null) {
return getLeftMost(node.right);
} else {
// No right subtree
Node parent = node.parent;
while (parent != null && parent.right == node) {
// The current node is the right child of its parent node
node = parent;
parent = node.parent;
}
return parent;
}
}
public static Node getLeftMost(Node node) {
if (node == null) {
return node;
}
while (node.left != null) {
node = node.left;
}
return node;
}
public static void main(String[] args) {
Node head = new Node(6);
head.parent = null;
head.left = new Node(3);
head.left.parent = head;
head.left.left = new Node(1);
head.left.left.parent = head.left;
head.left.left.right = new Node(2);
head.left.left.right.parent = head.left.left;
head.left.right = new Node(4);
head.left.right.parent = head.left;
head.left.right.right = new Node(5);
head.left.right.right.parent = head.left.right;
head.right = new Node(9);
head.right.parent = head;
head.right.left = new Node(8);
head.right.left.parent = head.right;
head.right.left.left = new Node(7);
head.right.left.left.parent = head.right.left;
head.right.right = new Node(10);
head.right.right.parent = head.right;
Node test = head.left.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.left.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.left.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.right; // 10's next is null
System.out.println(test.value + " next: " + getSuccessorNode(test));
}
}
边栏推荐
- 如何为 Arduino IDE 安装添加库
- Centos7部署radius服务-freeradius-3.0.13-15.el7集成mysql
- [focus on growth and build a dream for the future] - TDP year-end event, three chapters go to the Spring Festival!
- About the error of installing PIP3 install chatterbot
- Ant Usage Summary (II): description of related commands
- mongodb 4.x绑定多个ip启动报错
- Visual Studio调试技巧
- Wireshark TS | 视频 APP 无法播放问题
- App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
- 【DaVinci Developer专题】-42-如何生成APP SWC的Template和Header文件
猜你喜欢

jvm-04. Object's memory layout

Cloud native database is the future

Infotnews | which Postcard will you receive from the universe?

Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch

三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会

mysql以逗号分隔的字段作为查询条件怎么查——find_in_set()函数

jvm-03.jvm内存模型

mysql读已提交和可重复度区别

Wireshark TS | 视频 APP 无法播放问题

Prometheus, incluxdb2.2 installation and flume_ Export download compile use
随机推荐
Pat class B 1020 Moon Cake
Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch
Pat class B 1010 C language
[open source project] excel export Lua configuration table tool
True MySQL interview question (21) - Finance - overdue loan
Newbeecoder. Page animation switching of UI control library
Pat class B 1023 minimum decimals
100-300 cases of single chip microcomputer program (detailed explanation of notes)
mongodb 4.x绑定多个ip启动报错
Pat class B 1022 d-ary a+b
Explanation of penetration test process and methodology (Introduction to web security 04)
True MySQL interview question (24) -- row column exchange
The construction of digital factory can be divided into three aspects
【DaVinci Developer专题】-41-APP SWC如何读取写入NVM Block数据
Dolphin scheduler dolphin scheduling upgrade code transformation -upgradedolphin scheduler
Cryptography series: certificate format representation of PKI X.509
SQL表名与函数名相同导致SQL语句错误。
Pat class B 1013 C language
Implementation of linear list linked list structure
Eight data analysis models: ogsm model