Interview question 8: Next node of a binary tree

Source: Internet
Author: User
I. Questions

Given a binary tree and one of its nodes, how can we find the next node in the ascending traversal order? In addition to two pointers pointing to the left and right child nodes, the nodes in the tree also have a pointer pointing to the parent node.

Ii. Ideas

After analyzing the questions, we found that the nodes to be processed only have three states. We can process them separately:

  1. The node to be processed has the right subtree. Then its next node is the leftmost child node of its right subtree, for example, the next node of Node 3 is 8;
  2. The node to be processed has no right subtree and is the left node of the parent node. Then its next node is its parent node, for example, node 5's next node is 3;
  3. The node to be processed has no right subtree and is the right node of the parent node. We can traverse up along its parent node until we find such a node, which is the left child of its parent node, then the parent node of the node is the requested node, for example, the next node of node 6 is 1.
Iii. Code
BinaryTreeNode* GetNext(BinaryTreeNode* pNode){        if(pNode == nullptr)        return nullptr;    BinaryTreeNode* pNext = nullptr;    if(pNode->m_pRight != nullptr){        BinaryTreeNode* pRight = pNode->m_pRight;        while(pRight->m_pLeft != nullptr)            pRight = pRight->m_pLeft;        pNext = pRight;    }    else if(pNode->m_pParent != nullptr){        BinaryTreeNode* pCurrent = pNode;        BinaryTreeNode* pParent = pNode->m_pParetn;        while(pParent != nullptr && pNode = pParent->m_pRight){            pCurrent = pParent;            pParent = pParent->m_pParent;        }        pNext = pParent;    }    return pNext;}
4. Question site
  1. Measure the test taker's knowledge about binary tree traversal. Only by having a deep understanding of the binary tree traversal algorithm can the applicant accurately find the next node for the central traversal of each node.
  2. Measure the test taker's knowledge about complex problem analysis. Candidates can design a feasible algorithm only by drawing a binary tree structure and finding the law of the next node in the middle-order traversal through specific examples.

Interview question 8: Next node of a binary tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.