当前位置:网站首页>[jzof] 08 next node of binary tree
[jzof] 08 next node of binary tree
2022-07-23 13:19:00 【Sighed, angry】
describe :
Given a binary tree, one of its nodes , Please find the next node in the middle order traversal order and return . Be careful , The nodes in the tree contain not only left and right child nodes , It also contains the... Pointing to the parent node next The pointer . The following picture shows a tree with 9 A binary tree of nodes . The pointer from the parent node to the child node in the tree is represented by a solid line , Points from the child node to the parent node are indicated by dashed lines .

Answer key :
https://blog.nowcoder.net/n/737a842c39d6473db2a10d4f1de7610c?f=comment
Method 1 :
Now find root, And then from root Start middle order traversal , Save the results of the middle order , Then traverse the result of the middle order again , Compare and output the next node of the current node .
Method 2 : Direct search by classification
Ideas :
Direct search is divided into three cases
1. If the given node has a right child node , Then the next node to be returned is the leftmost lower node of the right subtree .
2. If the given node has no right child nodes , And the current node is the left child node of its parent node , Returns its parent node .
3. If the given node has no right child nodes , And the current node is the right child node of its parent node , First, climb the tree along the upper left parent node , Climb until the current node is the left child of its parent node , The parent node is returned ; Or if the above conditions are not met, it returns NULL.
specific working means :
step 1: Judge whether the node conforms to the first point in the idea , The lower left node of the right subtree is always found as the return value
step 2: Judge whether this node conforms to the second point in the idea , Then return the parent node of the current node
step 3: Judge whether this node conforms to the third point in the idea , Then iterate upward to find the parent node , Until the current node of the iteration is the left child node of the parent node , Return to the parent node ; If the above conditions are not met, return NULL
import java.util.*;
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode) {
// Situation 1
if(pNode.right != null) {
TreeLinkNode rchild = pNode.right;
// Always find the bottom left node of the right subtree as the return value
while(rchild.left != null) rchild = rchild.left;
return rchild;
}
// Situation two
if(pNode.next != null && pNode.next.left == pNode) {
return pNode.next;
}
// Situation three
if(pNode.next != null) {
TreeLinkNode ppar = pNode.next;
// Climb the tree along the top left , Climb until the current node is the left node of its parent node
while(ppar.next != null && ppar.next.right == ppar) ppar = ppar.next;
return ppar.next;
}
return null;
}
}
边栏推荐
猜你喜欢

Shooting lesson 1-3: image Sprite

第七天笔记

Numpy: quick start to basic operations

Redis如何实现持久化?详细讲解RDB的三种触发机制及其优缺点,带你快速掌握RDB

Beihui information is 12 years old | happy birthday

ZABBIX monitoring detailed installation to deployment

Uncaught (in promise) Neo4jError: WebSocket connection failure. Due to security constraints in your

Record a reptile question bank

UI自动化

Are there any academic requirements for career transfer software testing? Is there really no way out below junior college?
随机推荐
Beifu and C transmit real type through ads communication
Beifu PLC and C transmit bool type variables through ads communication
Numpy: element selection of matrix
第十二天笔记
Opencv image processing (medium) image smoothing + histogram
What happens when you enter the web address and the web page is displayed
Current limiting based on redis+lua
How to prevent repeated payment of orders?
Is it safe to open an account with Guosen Securities software? Will the information be leaked?
Shooting lesson 1-01: Introduction
成功 万象奥科与CODESYS技术联合调测
Talk about "people" in the R & D team
Complex networks - common drawing software and libraries
[actf2020 freshman competition]backupfile 1
倍福和C#通过ADS通信传输Real类型
倍福PLC和C#通过ADS通信传输bool类型变量
Convert the specified seconds to minutes and seconds
信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)
Intercept the specified range data set from list < map >
将指定秒转为时分秒格式