1. 程式人生 > 實用技巧 >資料結構之樹(其一)

資料結構之樹(其一)

/**
 * 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 {
    private List<Integer> ans = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        preorder(root);
        return ans;
    }
    public void preorder(TreeNode root) {
        
if (root == null) return; ans.add(root.val); preorder(root.left); preorder(root.right); } }
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null) return ans;
        LinkedList
<TreeNode> st = new LinkedList<>(); st.addLast(root); while(!st.isEmpty()) { TreeNode temp = st.removeLast(); ans.add(temp.val); if (temp.right != null) st.addLast(temp.right); if (temp.left != null) st.addLast(temp.left); } return ans; } }

統一模板(建議使用) 仿遞迴 解析

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        LinkedList<TreeNode> st = new LinkedList<>();
        TreeNode temp = root;
        while(temp != null || !st.isEmpty()) {
            while (temp != null) {
                st.addLast(temp);
                ans.add(temp.val);
                temp = temp.left;
            } 
            temp = st.removeLast();
            temp = temp.right;
        }
        return ans;
    }
}

中序遍歷 題目 解析

寫中序遍歷的時候已經忘了前序遍歷的迭代怎麼寫了。

遞迴和上面一個模板,不寫了。迭代也是仿遞迴的統一模板,先不斷把左子節點放入棧直到空節點。然後出棧左節點,把右節點放進去。

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        LinkedList<TreeNode> st = new LinkedList<>();
        TreeNode temp = root;
        while (!st.isEmpty() || temp != null) {
            while(temp != null) {
                st.addLast(temp);
                temp = temp.left;
            }
            temp = st.removeLast();
            ans.add(temp.val);
            temp = temp.right;
        }
        return ans;
    }
}

後序遍歷 題目 解析

先序遍歷根右左,反轉為左右根。

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root == null) return ans;
        LinkedList<TreeNode> st = new LinkedList<>();
        st.addLast(root);
        while(!st.isEmpty()) {
            TreeNode temp = st.removeLast();
            ans.add(temp.val);
            if (temp.left != null) st.addLast(temp.left);
            if (temp.right != null) st.addLast(temp.right);
        }
        Collections.reverse(ans);
        return ans;
    }
}

統一模板(建議使用)

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        LinkedList<TreeNode> st = new LinkedList<>();
        TreeNode temp = root;
        while (temp != null || !st.isEmpty()) {
            while (temp != null) {
                st.addLast(temp);
                ans.add(temp.val);
                temp = temp.right;
            }
            temp = st.removeLast();
            temp = temp.left;
        }
        Collections.reverse(ans);
        return ans;
    }
}