10-lt-二叉树的遍历

今天刚做了二叉树的前序遍历在这里总结一下二叉树遍历,利用递归解法。

144.二叉树的前序遍历

94.中序遍历

145.二叉树后序遍历

递归思想打掉二叉树遍历操作:

前序遍历

/**
 * 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> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();

        traversal(root,list);

        return list;

    }

    public void traversal(TreeNode root,List<Integer> list){
            if(root == null){
                return;
            }
            //根
            list.add(root.val);
            //左
            traversal(root.left,list);
            //右
            traversal(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> result = new ArrayList<>();
        traversal(root,result);
        return result;
    }
    public void  traversal(TreeNode root,List<Integer> list){
        if (root==null){
            return;
        }
        //左
        inorder(root.left,list);
        //根
        list.add(root.val);
        //右
        inorder(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> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        traversal(root,list);
        return list;
    }
    public void traversal(TreeNode root,List<Integer> list){
            if(root == null){
                return;
            }
            //左
            traversal(root.left,list);
            //右
            traversal(root.right,list);
            list.add(root.val);
    }
}

层次遍历

层次遍历是对二叉树进行一层一层的遍历

102.二叉树的层序遍历

/**
 * 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 {
    List<List<Integer>> levels = new ArrayList<List<Integer>>();
    public List<List<Integer>> levelOrder(TreeNode root) {
       if (root == null) return new ArrayList<>();
        levelOrderDepth(root, 0);
        return levels;
    }

    //进行层序遍历
    public void levelOrderDepth(TreeNode root,int level) {
        //如果当前levels的长度 和 level相等
        if (levels.size() == level) {
            levels.add(new ArrayList<>());
        }
        levels.get(level).add(root.val);

        if (root.left != null) {
            levelOrderDepth(root.left,level+1);
        }

        if (root.right != null) {
            levelOrderDepth(root.right,level+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 {
    public List<List<Integer>> levelOrder(TreeNode root) {
       List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }

        //队列操作
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            //进行遍历树
            List<Integer> list = new ArrayList<>();
            int len = queue.size();
            for (int i = 0;i<len; i++) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            res.add(list);
        }
        return res;
    }

}

上一篇:skynet里epoll为什么用lt模式


下一篇:用最新的采购信息记录更新采购单的价格——BAPI_PO_CHANGE