[Offer] wins the next node binary tree 8--

Title: Given a binary tree and a node which is, find the next node in a preorder traversal order and returns. Note that the node in the tree contains not only the left and right child nodes, the parent node contains a pointer pointing to.

 

A: As is inorder traversal, the left node -> the -> Right

 (1) a right subtree of the node: the next node is the left node of the right subtree of a node

 (2) the node does not have the right subtree is the parent node of the left: the next node is its parent node

 (3) The node has no right child, is the right parent node: traversing up, find a node is the left node of its parent node, the next node is the parent node of the node found

/ * 
Struct TreeLinkNode { 
    int Val; 
    struct TreeLinkNode * left; 
    struct * TreeLinkNode right; 
    struct * Next TreeLinkNode; // parent node 
    TreeLinkNode (int x): val ( x), left (NULL), right (NULL), next ( NULL) { 
        
    } 
}; 
* / 
class Solution { 
public: 
    TreeLinkNode * GetNext (TreeLinkNode * • pNode) 
    { 
        IF (== • pNode nullptr a) 
        { 
            return nullptr a; 
        } 
        // right subtree has: its next node, this node left and right node of the node 
        IF (pNode-> = nullptr a right!) 
        { 
            • pNode = pNode-> right; 
            the while (! pNode-> left = nullptr a) 
            {
                = pNode- • pNode> left; 
            } 
            return • pNode; 
        } 
        the while (pNode-> Next = nullptr a!) 
        { 
            // right subtree is not: The left node is the parent node, the next node is its parent node 
            if ( == pNode- • pNode> next-> left) 
            { 
                return pNode-> Next; 
            } 
            // right subtree is not: the node is the right node to its parent node, a parent node along the traverse up until you find a parent node left node 
            the else 
            { 
                • pNode = pNode-> Next; 
            } 
        } 
        return nullptr a; 
    } 
};

  

 

 

 

Related topics:

  Determine whether a full binary tree is a binary search tree: Given a full binary tree to determine whether the tree is a binary search tree, is then printed True, False print is not the case

  Binary: there are a binary tree, the tree each point mark is entitled to value weights vary, please design from the smallest leaf nodes of an algorithm to calculate the maximum weight of leaf nodes to the weights. From each side of a binary tree, a node of the distance between the two nodes after the number reaches the edges of the other node. Given binary tree root root, go back to ask away.

  Tree different forms: T given binary tree (tree depth not exceeding H <= 10, the depth from the beginning, the number of nodes N <1024, the node ID 1 ~ N) and the sequence preorder, from the left to the outputs T the right leaf node and the first tree traversal order and post-order sequence

  

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11415895.html