当前位置:网站首页>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));
}
}
边栏推荐
- True MySQL interview question (21) - Finance - overdue loan
- Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch
- Pat class B 1016 C language
- Gplearn appears assignment destination is read only
- Pit filling for abandoned openssl-1.0.2 (.A to.So)
- How to add libraries for Arduino ide installation
- Simple about fastdfs
- Pyinstaller sklearn报错的问题
- Implementation of linear list linked list structure
- Palindrome number for leetcode topic analysis
猜你喜欢

学习太极创客 — ESP8226 (十一)用 WiFiManager 库配网

jvm-05. garbage collection

Radar canvas

mongodb 4.x绑定多个ip启动报错

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

【开源项目】excel导出lua配置表工具

Explicability of counter attack based on optimal transmission theory

The hierarchyviewer tool cannot find the hierarchyviewer location

Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)

jvm-03. JVM memory model
随机推荐
jvm-06.垃圾回收器
pyinstaller 打包exe设置图标不显示
Excel sheet column number for leetcode topic resolution
Vant web app calendar component performance optimization calendar add min date the minimum date page loads slowly
Tencent security 2021 report white paper collection (download attached)
工作积累-判断GPS是否打开
Real MySQL interview question (23) -- pinduoduo ball game analysis
jvm-05.垃圾回收
关于安装pip3 install chatterbot报错的问题
Leetcode topic resolution integer to Roman
Android handler memory leak kotlin memory leak handling
node中操作mongoDB
WordPress contact form entries cross cross site scripting attack
Pat class B 1012 C language
Activity startup mode and life cycle measurement results
[cocos2d-x] screenshot sharing function
给定二叉树的某个节点,返回该节点的后继节点
Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)
ant使用总结(二):相关命令说明
Pyinstaller package exe setting icon is not displayed