当前位置:网站首页>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 :

  1. 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 ;
  2. 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));
	}

}
原网站

版权声明
本文为[Bright morning light]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230430397487.html