Binary Tree的题目总结(二)

这篇文章列出了leetcode中有关二叉树遍历的题目,之前在 二叉树的深搜和广搜中介绍过,这里再重复一下,因为这都是最基本的操作,需要我们熟练掌握。

1,Binary Tree Level Order Traversal
二叉树的广度优先搜索,输出所有节点的值,说的广搜,我们一律用队列来完成,代码如下:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> llist = new ArrayList<List<Integer>>();
        List<Integer> list = new ArrayList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        int count = 0;
        int helper = 1;
        if(root == null) return llist;
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if(helper > 0){
                list.add(node.val);
                helper --;
            }
            if(node.left != null) {
                queue.offer(node.left);
                count ++;
            }
            if(node.right != null) {
                queue.offer(node.right);
                count ++;
            }
            
            if(helper == 0) {
                llist.add(new ArrayList<Integer>(list));
                list.clear();
                helper = count;
                count = 0;
            }
        }
        return llist;
    }
}


2,Binary Tree Level Order Traversal II
给定二叉树:{3,9,20,#,#,15,7},
输出:[[15,7], [9,20], [3]]

这是第一题的变形,仅仅把结果逆序输出,我们用链表中的addFirst()方法就解决了。
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> llist = new LinkedList<List<Integer>>();
        List<Integer> list = new ArrayList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if(root == null) return llist;
        queue.offer(root);
        int count = 0;
        int helper = 1;
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if(helper > 0) {
                list.add(node.val);
                helper --;
            }
            if(node.left != null) {
                queue.offer(node.left);
                count ++;
            }
            if(node.right != null) {
                queue.offer(node.right);
                count ++;
            }
            
            if(helper == 0) {
                llist.addFirst(new ArrayList<Integer>(list));
                list.clear();
                helper = count;
                count = 0;
            }
        }
        return llist;
    }
}


下面是二叉树的前序遍历,中序遍历,后序遍历,都属于深搜,我们用堆栈来完成。

3,Binary Tree Inorder Traversal
二叉树的中序遍历,输出所有节点的值。

代码如下:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if(root == null) return list;
        while(root != null || !stack.isEmpty()) {
            if(root != null) {
                stack.push(root);
                root = root.left;
            } else {
                TreeNode node = stack.pop();
                list.add(node.val);
                root = node.right;
            }
        }
        return list;
    }
}


4,Binary Tree Preorder Traversal
二叉树的前序遍历,输出所有节点的值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if(root == null) return list;
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode tem = stack.pop();
            list.add(tem.val);
            if(tem.right != null) {
                stack.push(tem.right);
            }
            if(tem.left != null) {
                stack.push(tem.left);
            }
        }
        return list;
    }
}


5,Binary Tree Postorder Traversal
二叉树的后序遍历,输出所有节点的值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if(root == null) return list;
        stack.push(root);
        while(!stack.isEmpty()) {
            TreeNode node = stack.pop();
            list.addFirst(node.val);
            if(node.left != null) {
                stack.push(node.left);
            }
            if(node.right != null) {
                stack.push(node.right);
            }
        }
        return list;
    }
}

你可能感兴趣的:(java,二叉树遍历)