算法修炼Day14|● 递归遍历 ● 迭代遍历

66 阅读1分钟
题目:144. 二叉树的前序遍历 - 力扣(LeetCode)
思路/想法:

方法一:前序遍历:中-左-右。

对root节点进行判空,如果为空则直接返回,不为空则将节点值加入结果集中。

方法二

迭代法遍历。

代码实现:
// 方法一:
class Solution {
    List<Integer> ans = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        if (root == null) {
            return ans;
        } else {
            ans.add(root.val);
        }
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return ans;
    }
}

// 方法二:
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        // 前序:中左右  入栈:中 右 左
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root == null) {
            return ans;
        }
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            ans.add(node.val);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
        return ans;
    }
}
题目:145. 二叉树的后序遍历 - 力扣(LeetCode)
思路/想法:

后序遍历:左右中的顺序。

代码实现:
// 方法一:
class Solution {
    List<Integer> ans = new ArrayList<>();
    public List<Integer> postorderTraversal(TreeNode root) {
        // 后序:左右中
        if (root == null) {
            return ans;
        }
        postorderTraversal(root.left);
        postorderTraversal(root.right);
        if (root == null) {
            return ans;
        } else {
            ans.add(root.val);
        }
        return ans;
    }
}
// 方法二:
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        // 中序遍历:左 中 右  入栈:左 右
        List<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root == null) {
            return ans;
        }
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) { // 左
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop(); // 中
                ans.add(cur.val); 
                cur = cur.right; // 右
            }
        }
        return ans;
    }
}
题目:94. 二叉树的中序遍历 - 力扣(LeetCode)
思路/想法:

遍历顺序:左中右。

代码实现:
// 方法一:
class Solution {
    List<Integer> ans = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if (root == null) {
            return ans;
        }
        inorderTraversal(root.left);
        if (root != null) {
            ans.add(root.val);
        }
        inorderTraversal(root.right);
        return ans;
    }
}

// 方法二:

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        // 后序遍历:左右中 而前序遍历是:中左右。则将前序遍历结果反转一下即为后序遍历结果
        List<Integer> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            ans.add(node.val);
            if (node.left != null) {
                stack.push(node.left);
            }
            if (node.right != null) {
                stack.push(node.right);
            }
        }
        Collections.reverse(ans);
        return ans;
    }
}