Java binary tree inorder traversal (recursive/non-recursive)

Java binary tree inorder traversal (recursive/non-recursive)

Introduction: Traversal is one of the most basic operations on the tree. The so-called traversal of the binary tree is to walk through all the nodes of the binary tree according to certain rules and order, so that each node is visited once and only once.

Let L, D, and R represent traversing the left subtree, visiting the root node, and traversing the right subtree respectively, then there are several situations for traversing a binary tree: DLR (called pre-order traversal), LDR (called in-order traversal) traversal), LRD (called post-order traversal), level traversal.

Inorder traversal

Inorder traversal: first traverse the left subtree, then visit the root node, and finally traverse the right subtree.

As shown in the figure, the inorder traversal result of the binary tree is: [9,8,3,4,15,20,7]

insert image description here

Code

recursive way

public List<Integer> inorderTraversal(TreeNode root) {
    
    
    List<Integer> res = new ArrayList<Integer>();
    inorder(root, res);
    return res;
}

public void inorder(TreeNode root, List<Integer> res) {
    
    
    if (root == null) {
    
    
        return;
    }
    inorder(root.left, res);
    res.add(root.val);
    inorder(root.right, res);
}

non-recursive way

public List<Integer> inorderTraversal(TreeNode root) {
    
    
    List<Integer> res = new ArrayList<Integer>();
    Deque<TreeNode> stk = new LinkedList<TreeNode>();
    while (root != null || !stk.isEmpty()) {
    
    
        while (root != null) {
    
    
            stk.push(root);
            root = root.left;
        }
        root = stk.pop();
        res.add(root.val);
        root = root.right;
    }
    return res;
}

Full code:

public static void main(String[] args) {
    
    
    TreeNode root = new TreeNode(3);
    TreeNode n1 = new TreeNode(9);
    TreeNode n2 = new TreeNode(20);
    TreeNode n3 = new TreeNode(8);
    TreeNode n4 = new TreeNode(15);
    TreeNode n5 = new TreeNode(7);
    TreeNode n6 = new TreeNode(4);
    root.left = n1;
    root.right = n2;
    n1.right = n3;
    n2.left = n4;
    n2.right = n5;
    n4.left = n6;

    List<Integer> rs = inorderTraversal(root);
    System.out.println("递归中序遍历结果:" + rs);

    rs = preorderTraversal2(root);
    System.out.println("非递归中序遍历结果:" + rs);
}

// 递归
public static List<Integer> inorderTraversal(TreeNode root) {
    
    
    List<Integer> res = new ArrayList<Integer>();
    inorder(root, res);
    return res;
}

public static void inorder(TreeNode root, List<Integer> res) {
    
    
    if (root == null) {
    
    
        return;
    }
    inorder(root.left, res);
    res.add(root.val);
    inorder(root.right, res);
}

// 非递归
public static List<Integer> inorderTraversal(TreeNode root) {
    
    
    List<Integer> res = new ArrayList<Integer>();
    Deque<TreeNode> stk = new LinkedList<TreeNode>();
    while (root != null || !stk.isEmpty()) {
    
    
        while (root != null) {
    
    
            stk.push(root);
            root = root.left;
        }
        root = stk.pop();
        res.add(root.val);
        root = root.right;
    }
    return res;
}

operation result:

Guess you like

Origin blog.csdn.net/tracydragonlxy/article/details/106374435