二叉树相关题目(不定时更新中)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Hollake/article/details/98612140

 

二叉树前序,中序,后序递归非递归代码:二叉树遍历

二叉树按层从左到右打印,从顶部到底部

102. 二叉树的层次遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null) return lists;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while ( !queue.isEmpty() ) {
            int cur = 0;
            int end = queue.size();
            ArrayList<Integer> list = new ArrayList<>();
            while ( cur < end ) {
                TreeNode temp = queue.pop();
                list.add(temp.val);
                if (temp.left != null) {
                    queue.add(temp.left);
                }
                if (temp.right != null) {
                    queue.add(temp.right);
                }
                cur++;
            }
            lists.add(list);
        }
        return lists;
    }
}

二叉树按层,从底部到顶部

107. 二叉树的层次遍历 II

    public List<List<Integer>> levelOrder(TreeNode root) {
        LinkedList<List<Integer>> lists = new LinkedList<>();
        if (root == null) return lists;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while ( !queue.isEmpty() ) {
            int cur = 0;
            int end = queue.size();
            ArrayList<Integer> list = new ArrayList<>();
            while ( cur < end ) {
                TreeNode temp = queue.pop();
                list.add(temp.val);
                if (temp.left != null) {
                    queue.add(temp.left);
                }
                if (temp.right != null) {
                    queue.add(temp.right);
                }
                cur++;
            }
            lists.push(list);
        }
        return lists;
    }

二叉树深度

104. 二叉树的最大深度

    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int level = 0;
        while ( !queue.isEmpty() ) {
            int cur = 0;
            int end = queue.size();
            while ( cur < end ) {
                TreeNode temp = queue.pop();
                cur++;
                if (temp.left != null) {
                    queue.add(temp.left);
                }
                if (temp.right != null) {
                    queue.add(temp.right);
                }
            }
            level++;
        }
        return level;
    }

递归版

    public int TreeDepth(TreeNode root) {
        if(root == null) return 0;
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        return Math.max(left, right) + 1;
    }

二叉树之字形打印

103. 二叉树的锯齿形层次遍历

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            List<List<Integer>> lists = new ArrayList<>();
        if (root == null) return lists;
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.add(root);
        int level = 1;
        while ( !stack1.isEmpty() || !stack2.isEmpty() ) {
            List<Integer> list = new ArrayList<>();
            if (level % 2 == 1) {
                while ( !stack1.isEmpty() ) {
                    TreeNode temp = stack1.pop();
                    list.add(temp.val);
                    if (temp.left != null) {
                        stack2.push(temp.left);
                    }
                    if (temp.right != null) {
                        stack2.push(temp.right);
                    }
                }
            }else {
                while ( !stack2.isEmpty() ) {
                    TreeNode temp = stack2.pop();
                    list.add(temp.val);
                    if (temp.right != null) {
                        stack1.push(temp.right);
                    }
                    if (temp.left != null) {
                        stack1.push(temp.left);
                    }

                }
            }
            lists.add(list);
            level++;
        }
        return lists;
    }

对称二叉树

101. 对称二叉树

    public boolean isSymmetric(TreeNode root) {
        return isSymmetric(root, root);
    }

    private boolean isSymmetric(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return true;
        }
        if (root1 == null || root2 == null) {
            return false;
        }
        if (root1.val != root2.val) {
            return false;
        }
        return isSymmetric(root1.left, root2.right) && isSymmetric(root1.right, root2.left);
    }

翻转的二叉树(镜像二叉树)

226. 翻转二叉树

    public TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }
    // 镜像二叉树
    public void mirror(TreeNode root) {
        if (root == null) return;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        if (root.left != null) {
            mirror(root.left);
        }
        if (root.right != null) {
            mirror(root.right);
        }
    }

判断是否是平衡二叉树

110. 平衡二叉树

    public boolean isBalanced(TreeNode root) {
        
        if(root == null) return true;
//        先判断是不是左树和右树的高度差是不是1,接着判断左树和右树是不是平衡二叉树。
        if (Math.abs(getDeep(root.left) - getDeep(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right)) {
            return true;
        }else {
            return false;
        }
    }
    private int getDeep(TreeNode root) {
        if (root == null) return 0;
        int left = getDeep(root.left);
        int right = getDeep(root.right);
        return (left > right ? left : right) + 1;
    }

猜你喜欢

转载自blog.csdn.net/Hollake/article/details/98612140