当前位置:网站首页>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));
}
}
边栏推荐
- Pat class B 1016 C language
- jvm-01.指令重排
- Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference
- 微软面试题:打印折纸的折痕
- Basic calculator for leetcode topic analysis
- mongodb 4.x绑定多个ip启动报错
- Kotlin collaboration +retro most elegant network request use
- 【Cocos2d-x】自定义环形菜单
- The traditional Internet like platform may no longer exist, and a new industry integrating industrial characteristics and Internet characteristics
- Wireshark TS | 视频 APP 无法播放问题
猜你喜欢

New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!

JVM原理简介

Redis 哨兵

ant使用总结(二):相关命令说明

ant使用总结(一):使用ant自动打包apk

Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference

True MySQL interview question (XXII) -- condition screening and grouping screening after table connection
![[open source project] excel export Lua configuration table tool](/img/3a/8e831c4216494d5497928bae21523b.png)
[open source project] excel export Lua configuration table tool

Centos7 deploy radius service -freeradius-3.0.13-15 EL7 integrating MySQL

内存分析与内存泄漏检测
随机推荐
Matplotlib savefig multiple picture overlay
Pat class B 1009 C language
又到半年总结时,IT人只想躺平
Leetcode topic analysis add binary
去除防火墙和虚拟机对live555启动IP地址的影响
Layer 2技术方案进展情况
金融科技之高效办公(一):自动生成信托计划说明书
SSM project construction
The construction of digital factory can be divided into three aspects
jvm-03. JVM memory model
Pyqt5 设置窗口左上角图标
jvm-02.有序性保证
Day_13 传智健康项目-第13章
Tcp/ip explanation (version 2) notes / 3 link layer / 3.3 full duplex, energy saving, automatic negotiation mechanism, 802.1x flow control / 3.3.3 link layer flow control
The difference between SaaS software and traditional software delivery mode
True MySQL interview question (XXII) -- condition screening and grouping screening after table connection
matplotlib savefig多个图片叠加问题
Android handler memory leak kotlin memory leak handling
mongodb 4.x绑定多个ip启动报错
Pat class B 1010 C language