BinaryTreePreorderTraversal

  • 2022-12-14
  • 浏览 (548)

BinaryTreePreorderTraversal.java 源码

package datastructure.tree.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author roseduan
 * @time 2020/9/15 9:02 下午
 * @description 二叉树前序遍历
 */
public class BinaryTreePreorderTraversal {

    private List<Integer> res = new ArrayList<>();

    /**
     * 递归
     */
    public List<Integer> preorderTraversal(TreeNode root) {
        if (root == null) {
            return res;
        }
        res.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return res;
    }

    /**
     * 迭代
     */
    public List<Integer> preorderTraversal2(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        stack.push(cur);
        while (!stack.empty()) {
            cur = stack.pop();
            list.add(cur.val);
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }
        return list;
    }

    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}

你可能感兴趣的文章

BinaryTreeInorderTraversal

BinaryTreeLevelOrderTraversal

BinaryTreePostorderTraversal

0  赞