Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to python the right pointer in a binary tree

Shulou Source: shulou.com Published: 2022-06-01 14:12:19 10月03日 Update

How to python the right pointer in the binary tree, I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Given a perfect binary tree, all the leaf nodes are in the same layer, and each parent node has two child nodes. The binary tree is defined as follows:

Struct Node {

Int val

Node * left

Node * right

Node * next

}

Populate each of its next pointers so that it points to its next right node. If the next right node cannot be found, set the next pointer to NULL.

In the initial state, all next pointers are set to NULL.

Example:

Enter: {"$id": "1", "left": {"$id": "2", "left": {"$id": "3", "left": null, "next": null, "right": null, "val": 4}, "next": null, "right": {"$id": "4", "left": null, "next": null, "right": null, "val": 5}, "val": 2}, "next": null "right": {"$id": "5", "left": {"$id": "6", "left": null, "next": null, "right": null, "val": 6}, "next": null, "right": {"$id": "7", "left": null, "next": null, "right": null, "val": 7}, "val": 3}, "val": 1}

Output: {"$id": "1", "left": {"$id": "2", "left": {"$id": "3", "left": null, "next": {"$id": "4", "left": null, "next": {"$id": "5", "left": null, "next": {"$id": "6", "left": null, "next": null, "right": null, "val": 7}, "right": Val: 6}, right: null, val: 5}, right: null, val: 4}, next: {"$id": "7", "left": {"$ref": "5"}, "next": null, "right": {"$ref": "6"}, "val": 3}, "right": {"$ref": "4"}, "val": 2}, "next": null, "right": {"$ref": "7"} "val": 1}

Explanation: given a binary tree as shown in figure A, your function should populate each of its next pointers to point to its next right node, as shown in figure B.

Ideas for solving the problem:

1. All tree problems are recursive.

2, fill in a layer first, and then deal with the left and right subtrees recursively

3, because it is a perfect binary tree, it is easy to deal with:

The next of the left subtree is the right subtree, and the next of the ️subtree is the left subtree of next.

/ * / / Definition for a Node.class Node {public: int val; Node* left; Node* right; Node* next

Node () {}

Node (int _ val, Node* _ left, Node* _ right, Node* _ next) {val = _ val; left= _ left; right = _ right; next= _ next;}}; * / class Solution {public: Node* connect (Node* root) {if (root==NULL | | root- > left==NULL) {return root;} root- > left- > next=root- > right If (root- > nextsteps null) {root- > right- > next=root- > next- > left;} connect (root- > left); connect (root- > right); return root;}}

Given a binary tree

Struct Node {

Int val

Node * left

Node * right

Node * next

}

Populate each of its next pointers so that it points to its next right node. If the next right node cannot be found, set the next pointer to NULL.

In the initial state, all next pointers are set to NULL.

Example:

Enter: {"$id": "1", "left": {"$id": "2", "left": {"$id": "3", "left": null, "next": null, "right": null, "val": 4}, "next": null, "right": {"$id": "4", "left": null, "next": null, "right": null, "val": 5}, "val": 2}, "next": null "right": {"$id": "5", "left": null, "next": null, "right": {"$id": "6", "left": null, "next": null, "right": null, "val": 7}, "val": 3}, "val": 1}

Output: {"$id": "1", "left": {"$id": "2", "left": {"$id": "3", "left": null, "next": {"$id": "4", "left": null, "next": {"$id": "5", "left": null, "next": null, "right": null, "val": 7}, "right": null, "val": 5}, "right": null, "val": 4} "next": {"$id": "6", "left": null, "next": null, "right": {"$ref": "5"}, "val": 3}, "right": {"$ref": "4"}, "val": 2}, "next": null, "right": {"$ref": "6"}, "val": 1}

Explanation: given a binary tree as shown in figure A, your function should populate each of its next pointers to point to its next right node, as shown in figure B.

Tip:

You can only use constant order of magnitude extra space.

The use of recursive problem solving also meets the requirements, and the stack space occupied by recursive programs in this problem does not count as additional space complexity.

Ideas for solving the problem:

1. The only difference between this question and the previous one is whether it is a perfect binary tree.

2, so you need to calculate what the next node is:

A, left subtree (if not empty)

B, right subtree (if not empty)

The subtree of CMagneNext

3. There are two cases of the next node of the left subtree:

A, right subtree (if not empty)

Subtree of BMagneNext (or next of next) node

4, the next node of the right subtree: the subtree of the next (or next of next) node

5. Since the calculation of next nodes depends on the right subtree, the right subtree is recursive first.

/ * / / Definition for a Node.class Node {public: int val; Node* left; Node* right; Node* next

Node () {}

Node (int _ val, Node* _ left, Node* _ right, Node* _ next) {val = _ val; left = _ left; right = _ right; next = _ next;}}; * / class Solution {public: Node* connect (Node* root) {if (root==NULL) {return NULL } if (root- > rightshield null) {if (root- > rightshield null) {root- > left- > next=root- > right;} else {root- > left- > next=nextNode (root- > next);} if (root- > rightshield null) {root- > right- > next=nextNode (root- > next) } connect (root- > right); connect (root- > left); return root;} Node* nextNode (Node* root) {Node* tweeroot; while (tasking null) {if (t-> leftcircle null) {return t-> left;} if (t-> rightcircle null) {return t-> right } tweak-> next;} return NULL;}}; after reading the above, have you mastered how to python the right pointer in the binary tree? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Node subtree pointer right recursive pointing under it such as graph space problem content function idea method more this topic state example processing explanation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Xiaomi Shulou Tech Info OPPO Reno