construct binary tree 构造一棵二叉树

Posted 排序和map

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了construct binary tree 构造一棵二叉树相关的知识,希望对你有一定的参考价值。

参考:https://www.geeksforgeeks.org/binary-tree-set-1-introduction/

 

/* Class containing left and right child of current
node and key value*/
class Node
{
    int key;
    Node left, right;

    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}

// A Java program to introduce Binary Tree
class BinaryTree
{
    // Root of Binary Tree
    Node root;

    // Constructors
    BinaryTree(int key)
    {
        root = new Node(key);
    }

    BinaryTree()
    {
        root = null;
    }

    public static void main(String[] args)
    {
        BinaryTree tree = new BinaryTree();

        /*create root*/
        tree.root = new Node(1);

        /* following is the tree after above statement

            1
            / \\
        null null     */

        tree.root.left = new Node(2);
        tree.root.right = new Node(3);

        /* 2 and 3 become left and right children of 1
            1
            /     \\
        2     3
        / \\     / \\
    null null null null */


        tree.root.left.left = new Node(4);
        /* 4 becomes left child of 2
                    1
                /     \\
            2         3
            / \\     / \\
            4 null null null
        / \\
        null null
        */
    }
}

 

以上是关于construct binary tree 构造一棵二叉树的主要内容,如果未能解决你的问题,请参考以下文章

Construct Binary Tree

CF1311E Construct the Binary Tree

Construct Binary Tree from Preorder and Inorder Traversal -- LeetCode

LeetCode | 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树Python(示(代

[Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

LeetCode-面试算法经典-Java实现106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)(示例(代码片