当前位置:网站首页>[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;
}
}
边栏推荐
- Liveness, readiness and startup probes
- Static routing principle and configuration
- Introduction to JVM memory model
- TI单芯片毫米波雷达代码走读(二十五)—— 角度维(3D)处理流程
- Harbor deployment
- Signal integrity (SI) power integrity (PI) learning notes (32) power distribution network (4)
- JVM内存模型简介
- JVM详细解析
- 费曼学习法(Redis总结)
- 力扣 729. 我的日程安排表 I
猜你喜欢

将指定秒转为时分秒格式

Cortex-a series processor

EasyGBS平台出现录像无法播放并存在RTMP重复推流现象,是什么原因?

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

Functional testing to automated testing, sharing ten years of automated testing experience

Shooting games lesson 1-2: using sprites

课程设计-推箱子C#(win form)

MySQL----复合查询 外连接

How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB

设计思维的“布道者”
随机推荐
如何防止订单重复支付?
Beihui information is 12 years old | happy birthday
C语言-大端存储和小端存储
课程设计-推箱子C#(win form)
OpenCV图像处理(上)几何变换+形态学操作
The relationship between method area, perpetual generation and meta space
Introduction to JVM memory model
The unity model is displayed in front of the UI, and the UI behind it jitters
When using fastjson to parse and assign JSON data, the order of JSON fields is inconsistent
Intercept the specified range data set from list < map >
Paging collections using streams
根据不同时间统计不同类型的数据(存储过程)
复杂网络-常用绘图软件和库
Helm installing rancher
Evaluation of classification model
OpenCV 视频操作
JVM详细解析
Li Kou 729. My schedule I
【离线语音专题④】安信可VC离线语音开发板二次开发语音控制LED灯
Current limiting based on redis+lua