📌  相关文章
📜  在二叉树中下沉偶数节点

📅  最后修改于: 2022-05-13 01:57:17.039000             🧑  作者: Mango

在二叉树中下沉偶数节点

给定具有奇数和偶数元素的二叉树,将其所有偶值节点下沉,使得具有偶值的节点不能成为具有奇值的节点的父节点。

给定的树可以有多个输出,我们需要打印其中一个。总是可以转换一棵树(请注意,具有奇数节点的节点和所有偶数节点都遵循规则)

例子:

Input: 
       1
     /    \
    5       8
  /  \     /  \
 2    4   9    10
Output: 
1 
5 9 
2 4 8 10

Level order traversal after
sinking all the nodes

Input: 
  4
 /  \
2    1
Output: 
4
2 1
Explanation: 
In the first case
Given tree
       4
    /    \
   2      1

There are two trees possible
       1            1
    /    \   OR   /   \
   2      4      4     2 
  

In the second example also,
Given tree
       1
     /    \
    5       8
  /  \     /  \
 2    4   9    10

There are more than one tree
that can satisfy the condition
      1                 1
   /    \            /    \     
  5       9    OR   5      9   
 /  \    /  \      /  \   / \   
2   4  8   10    4    2  8  10

方法:

  • 基本上,需要将节点的偶数值与其后代之一的奇数值交换。
  • 这个想法是以后序方式遍历树。
  • 由于我们按后序处理,因此对于遇到的每个偶数节点,其左右子树已经平衡(下沉)。
  • 检查它是否是偶数节点,其左或右子节点是否为奇数。如果找到奇数,则将该节点的数据与奇数子节点的数据交换,并调用奇数子节点上的过程来平衡子树。
  • 如果两个孩子的值都是偶数,那就意味着他们所有的后代都是偶数。

下面是这个想法的实现:

Python
# Python3 program to sink even nodes
# to the bottom of binary tree
 
# A binary tree node
# Helper function to allocates a new node
class newnode:
 
    # Constructor to create a new node
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
 
# Helper function to check
# if node is leaf node
def isLeaf(root):
    return (root.left == None and
            root.right == None)
 
# A recursive method to sink a tree with even root
# This method assumes that the subtrees are
# already sinked. This method is similar to
# Heapify of Heap-Sort
def sink(root):
     
    # If None or is a leaf, do nothing
    if (root == None or isLeaf(root)):
        return
     
    # if left subtree exists and
    # left child is even
    if (root.left and (root.left.data & 1)):
         
        # swap root's data with left child
        # and fix left subtree
        root.data, root.left.data = root.left.data, root.data
        sink(root.left)
         
    # if right subtree exists and
    # right child is even
    elif(root.right and (root.right.data & 1)):
         
        # swap root's data with right child
        # and fix right subtree
        root.data, root.right.data = root.right.data, root.data
        sink(root.right)
 
# Function to sink all even nodes to
# the bottom of binary tree. It does
# a postorder traversal and calls sink()
# if any even node is found
def sinkevenNodes(root):
     
    # If None or is a leaf, do nothing
    if (root == None or isLeaf(root)):
        return
         
    # Process left and right subtrees
    # before this node
    sinkevenNodes(root.left)
    sinkevenNodes(root.right)
     
    # If root is even, sink it
    if not (root.data & 1):
        sink(root)
 
# Helper function to do Level Order Traversal
# of Binary Tree level by level. This function
# is used here only for showing modified tree.
def printLevelOrder(root):
    q = []
    q.append(root)
     
    # Do Level order traversal
    while (len(q)):
         
        nodeCount = len(q)
         
        # Print one level at a time
        while (nodeCount):
            node = q[0]
            print(node.data, end = " ")
            q.pop(0)
            if (node.left != None):
                q.append(node.left)
            if (node.right != None):
                q.append(node.right)
            nodeCount -= 1
         
        # Line separator for levels
        print()
 
# Driver Code
""" Constructed binary tree is
            1
        / \
        5 8
        / \ / \
    2 4 9 10     """
root = newnode(1)
root.left = newnode(5)
root.right = newnode(8)
root.left.left = newnode(2)
root.left.right = newnode(4)
root.right.left = newnode(9)
root.right.right = newnode(10)
 
sinkevenNodes(root)
 
printLevelOrder(root)
 
# This code is contributed by SHUBHAMSINGH10


Javascript


输出:
1 
5 9 
2 4 8 10