二叉树的前序,中序,后序遍历

给你二叉树的根节点 root ,返回它节点值的先序 ,中序,后序遍历。

先序:

递归代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    public static List<Integer> preorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        pre(root, list);
        return list;
    }

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

迭代:

步骤:

  1. 从栈中弹出一个结点cur。
  2. 打印cur。
  3. 先把cur的右孩子压入栈,再把cur的左孩子压入栈(如果有的话)
  4. 循环。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    public static List<Integer> preorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        pre(root, list);
        return list;
    }


    public static void pre(TreeNode root, List<Integer> list) {
    
    
        if (root == null) {
    
    
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
    
    
            TreeNode p = stack.pop();
            list.add(p.val);
            if (p.right != null) {
    
    
                stack.push(p.right);
            }
            if (p.left != null) {
    
    
                stack.push(p.left);
            }
        }
    }
}

中序:

递归:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        dfs(root, list);
        return list;
    }

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

迭代:

步骤:

先把整棵树的左边界全压入栈中,依次弹出结点的过程中,打印,对弹出结点的右节点周而复始。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        inorder(root, list);
        return list;
    }

    public static void inorder(TreeNode root, List<Integer> list) {
    
    
        if (root == null) {
    
    
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        while (!stack.isEmpty() || root != null) {
    
    
            if (root != null) {
    
    
                stack.push(root);
                root = root.left;
            } else {
    
    
                root = stack.pop();
                list.add(root.val);
                root = root.right;
            }
        }
    }
}

后序:

递归:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        pre(root, list);
        return list;
    }
    
    public static void pre(TreeNode root, List<Integer> list) {
    
    
        if (root == null) {
    
    
            return;
        }
        pre(root.left, list);
        pre(root.right, list);
        list.add(root.val);
    }
}

迭代:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        List<Integer> list = new ArrayList<>();
        postorder(root, list);
        return list;
    }
    
    public static void postorder(TreeNode root, List<Integer> list) {
    
    
        if (root == null) {
    
    
            return;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(root);
        while (!stack1.isEmpty()) {
    
    
            TreeNode p = stack1.pop();
            stack2.push(p);
            if (p.left != null) {
    
    
                stack1.push(p.left);
            }
            if (p.right != null) {
    
    
                stack1.push(p.right);
            }
        }
        while (!stack2.isEmpty()) {
    
    
            list.add(stack2.pop().val);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/FYPPPP/article/details/110009947