二叉树层次遍历

217 阅读1分钟

二叉树层次遍历

1. node方法

public class Node {

    int data;
    Node leftChild;
    Node rightChild;

    public Node(int data) {
        this.data = data;
    }

    public Node() {
    }
    
}

2. 二叉树的遍历

二叉树的遍历方法

  • 前序遍历
  • 中序遍历
  • 后序遍历
  • 层次遍历

3. 层次遍历

BinaryTree.java

public class BinaryTree{
    private Node root;
  
    /**
     * Description: insert data
     *
     * @param num 树结点数据
     * @return Node 
     * @exception
     * @throws
     *
     */
    @Override
    public Node insertNode(int num) {
        Node node = new Node(num);
        return this.insertNode(node);
    }
  
    // insert node  
    public Node insertNode(Node node) {
        if (root == null) {
            root = node;
            return node;
        }

        return insert(root, node);
    }


    private Node insert(Node rootNode, Node node) {
        if (rootNode == node) {
            return node;
        }

        Node tmp = new Node();
        tmp = rootNode;

        Node last = null;

        while (tmp != null) {
            last = tmp;
            if (tmp.data > node.data) {
                tmp = tmp.leftChild;
            } else {
                tmp = tmp.rightChild;
            }
        }

        if (last != null) {
            if (last.data > node.data) {
                last.leftChild = node;
            } else {
                last.rightChild = node;
            }
        }

        return root;
    }
  
  
    // 层次遍历
    public void levelIterator() {
        levelIterator(root);
    }

    private void levelIterator(Node node) {
        if (root == null) {
            return;
        }

        LinkedList<Node> queue = new LinkedList<Node>();

        Node current = null;
        queue.offer(node);

        while (!queue.isEmpty()) {
            current = queue.poll();
            System.out.print(current.data + " ");

            if (current.leftChild != null) {
                queue.offer(current.leftChild);
            }

            if (current.rightChild != null) {
                queue.offer(current.rightChild);
            }
        }
    }  

}

BinaryTreeTest.java

public class BinaryTreeTest {

    private BinaryTree bt = null;

    @Before
    public void init() {

        bt = new BinaryTree();

        bt.insertNode(50);
        bt.insertNode(20);
        bt.insertNode(80);
        bt.insertNode(10);
        bt.insertNode(30);
        bt.insertNode(60);
        bt.insertNode(90);
        bt.insertNode(25);
        bt.insertNode(85);
        bt.insertNode(100);

    }

    @Test
    public void Test(){
        bt.levelIterator();
    }
} 

4.leetcode606 - 根据二叉树创建字符串

输入: 二叉树: [1,2,3,4]
       1
     /   \
    2     3
   /    
  4     

输出: "1(2(4))(3)"

解释: 原本将是“1(2(4)())(3())”,
在你省略所有不必要的空括号对之后,
它将是“1(2(4))(3)”。
/**
     * Description: “1(2(4))(3)”
     *
     * @param
     * @return
     * @exception
     * @throws
     *
     */
    @Override
    public String binaryTreeToString() {

        StringBuilder stringBuilder = new StringBuilder();
        if (root == null) {
            return stringBuilder.toString();
        }

        binaryTreeToString(root, stringBuilder);

        return stringBuilder.toString();
    }

    private void binaryTreeToString(Node node, StringBuilder stringBuilder) {

        if (node == null) {
            return ;
        }

        stringBuilder.append(node.data);

        if (node.leftChild != null) {
            stringBuilder.append("(");
            binaryTreeToString(node.leftChild, stringBuilder);
            stringBuilder.append(")");
        } else {
            if (node.rightChild != null) {
                stringBuilder.append("()");
            }
        }

        if (node.rightChild != null) {
            stringBuilder.append("(");
            binaryTreeToString(node.rightChild, stringBuilder);
            stringBuilder.append(")");
        }
    }