📌  相关文章
📜  使用给定的后序遍历在二叉树中查找给定节点的父节点

📅  最后修改于: 2021-09-06 06:36:04             🧑  作者: Mango

给定两个整数NK ,其中 N 表示二叉树的高度,任务是在二叉树中找到值为 K 的节点的父节点,其后序遍历是第一

2^{N}-1

自然数

(1, 2, ... 2^{N}-1)

For N = 3, the Tree will be -

      7
    /   \
   3     6
 /   \  /  \
1     2 4   5

例子:

朴素方法:一种简单的方法是根据以下模式构建树,然后遍历整棵树以找到给定节点的父节点。
有效的方法:这个想法是使用二分搜索来找到节点的父节点。我们知道高度为 N 的二叉树有

2^{N}-1

节点。因此,二分搜索的搜索空间将为 1 到

2^{N}-1

现在每个节点都有子值

\frac{X}{2}

或者

X-1

因此,可以轻松找到此类节点的父节点。
下面是上述方法的实现:

C++
// C++ implementation to find the
// parent of the given node K in
// a binary tree whose post-order
// traversal is N natural numbers
 
#include 
using namespace std;
 
// Function to find the parent
// of the given node
int findParent(int height, int node)
{
    int start = 1;
    int end = pow(2, height) - 1;
 
    // Condition to check whether
    // the given node is a root node.
    // if it is then return -1 because
    // root node has no parent
    if (end == node)
        return -1;
 
    // Loop till we found
    // the given node
    while (node >= 1) {
        end = end - 1;
 
        // Finding the middle node of the
        // tree because at every level
        // tree parent is
        // divided into two halves
        int mid = start
                  + (end - start)
                        / 2;
 
        // if the node is found return
        // the parent always the child
        // nodes of every node
        // is node/2 or (node-1)
        if (mid == node || end == node) {
            return (end + 1);
        }
 
        // if the node to be found
        // is greater than the mid
        // search for left subtree else
        // search in right subtree
        else if (node < mid) {
            end = mid;
        }
        else {
            start = mid;
        }
    }
}
 
// Driver Code
int main()
{
    int height = 4;
    int node = 6;
 
    int k = findParent(height, node);
    cout << k;
 
    return 0;
}


Java
// Java implementation to find the
// parent of the given node K in
// a binary tree whose post-order
// traversal is N natural numbers
import java.util.*;
class GFG{
 
// Function to find the parent
// of the given node
static int findParent(int height,
                      int node)
{
  int start = 1;
  int end = (int)Math.pow(2, height) - 1;
 
  // Condition to check whether
  // the given node is a root node.
  // if it is then return -1 because
  // root node has no parent
  if (end == node)
    return -1;
 
  // Loop till we found
  // the given node
  while (node >= 1)
  {
    end = end - 1;
 
    // Finding the middle node of the
    // tree because at every level
    // tree parent is
    // divided into two halves
    int mid = start + (end - start) / 2;
 
    // if the node is found return
    // the parent always the child
    // nodes of every node
    // is node*/2 or (node-1)
    if (mid == node || end == node)
    {
      return (end + 1);
    }
 
    // if the node to be found
    // is greater than the mid
    // search for left subtree else
    // search in right subtree
    else if (node < mid)
    {
      end = mid;
    }
    else
    {
      start = mid;
    }
  }
  return -1;
}
 
// Driver Code
public static void main(String[] args)
{
  int height = 4;
  int node = 6;
  int k = findParent(height, node);
  System.out.print(k);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python implementation to find the
# parent of the given node
 
import math
 
# Function to find the parent
# of the given node
def findParent(height, node):
 
    start = 1
    end = pow(2, height) - 1
 
    # Check whether the given node
    # is a root node.if it is then
    # return -1 because root
    # node has no parent
    if (end == node):
        return -1
 
    # Loop till we found
    # the given node
    while(node >= 1):
 
        end = end - 1
 
        # Find the middle node of the
        # tree because at every level
        # tree parent is divided
        # into two halves
        mid = start + (end - start)//2
 
        # if the node is found
        # return the parent
        # always the child nodes of every
        # node is node / 2 or (node-1)
        if(mid == node or end == node):
            return (end + 1)
         
        # if the node to be found is greater
        # than the mid search for left
        # subtree else search in right subtree
        elif (node < mid):
            end = mid
 
        else:
            start = mid
 
# Driver code
if __name__ == "__main__":
    height = 4
    node = 6
     
    # Function Call
    k = findParent(height, node)
    print(k)


C#
// C# implementation to find the
// parent of the given node K in
// a binary tree whose post-order
// traversal is N natural numbers
using System;
class GFG{
 
// Function to find the parent
// of the given node
static int findParent(int height,
                      int node)
{
  int start = 1;
  int end = (int)Math.Pow(2, height) - 1;
 
  // Condition to check whether
  // the given node is a root node.
  // if it is then return -1 because
  // root node has no parent
  if (end == node)
    return -1;
 
  // Loop till we found
  // the given node
  while (node >= 1)
  {
    end = end - 1;
 
    // Finding the middle node of the
    // tree because at every level
    // tree parent is
    // divided into two halves
    int mid = start + (end - start) / 2;
 
    // if the node is found return
    // the parent always the child
    // nodes of every node
    // is node*/2 or (node-1)
    if (mid == node || end == node)
    {
      return (end + 1);
    }
 
    // if the node to be found
    // is greater than the mid
    // search for left subtree else
    // search in right subtree
    else if (node < mid)
    {
      end = mid;
    }
    else
    {
      start = mid;
    }
  }
  return -1;
}
 
// Driver Code
public static void Main(String[] args)
{
  int height = 4;
  int node = 6;
  int k = findParent(height, node);
  Console.Write(k);
}
}
 
// This code is contributed by Princi Singh


输出:
7





如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live