LeetCodeが「Sword Finger Offer」と再び戦い、ツリー関連(1)

みなさん、こんにちは。私は方圆
持ってませんが、慣れています


ソードフィンガーオファー07.バイナリツリーの再構築

ここに画像の説明を挿入

class Solution {
    
    
    public TreeNode buildTree(int[] preorder, int[] inorder) {
    
    
        ArrayList<Integer> pre = new ArrayList<>();
        ArrayList<Integer> in = new ArrayList<>();

        for(int i : preorder) pre.add(i);
        for(int j : inorder) in.add(j);

        return helper(pre,in);
    }

    private TreeNode helper(List<Integer> pre,List<Integer> in){
    
    
        if(pre.size() == 0) return null;

        int rootVal = pre.get(0);
        TreeNode root = new TreeNode(rootVal);
        int index = in.indexOf(rootVal);

        root.left = helper(pre.subList(1,1 + index),in.subList(0,index));
        root.right = helper(pre.subList(1 + index,pre.size()),in.subList(index + 1,in.size()));

        return root;
    }
}

ソードはオファー26を指します。ツリーのサブ構造

ここに画像の説明を挿入

class Solution {
    
    
    public boolean isSubStructure(TreeNode A, TreeNode B) {
    
    
        if(A == null || B == null) return false;

        return helper(A,B) || isSubStructure(A.left,B) || isSubStructure(A.right,B);
    }

    private boolean helper(TreeNode A,TreeNode B){
    
    
        if(B == null) return true;
        if(A == null || A.val != B.val) return false;
        return helper(A.left,B.left) && helper(A.right,B.right);
    }
}

ソードはオファー27を指します。二分木の鏡像

ここに画像の説明を挿入

class Solution {
    
    
    public TreeNode mirrorTree(TreeNode root) {
    
    
        if(root == null) return null;

        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        mirrorTree(root.left);
        mirrorTree(root.right);

        return root;
    }
}

剣指オファー28.対称二分木

ここに画像の説明を挿入

class Solution {
    
    
    public boolean isSymmetric(TreeNode root) {
    
    
        if(root == null) return true;

        return helper(root.left,root.right);
    }

    private boolean helper(TreeNode a,TreeNode b){
    
    
        if(a == null && b == null) return true;

        if((a == null || b == null) || a.val != b.val) 
            return false;
        
        return helper(a.left,b.right) && helper(a.right,b.left);
    }
}

ソードフィンガーオファー32-I。バイナリツリーを上から下に印刷

ここに画像の説明を挿入

class Solution {
    
    
    public int[] levelOrder(TreeNode root) {
    
    
        if(root == null) return new int[0];

        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(queue.size() > 0){
    
    
            TreeNode temp = queue.poll();
            res.add(temp.val);
            if(temp.left != null) queue.add(temp.left);
            if(temp.right != null) queue.add(temp.right);
        }
        int[] ans = new int[res.size()];
        for(int i = 0;i < res.size();i++)
            ans[i] = res.get(i);

        return ans;
    }
}

剣はオファー32-IIを指します。バイナリツリーを上から下に印刷してくださいII

ここに画像の説明を挿入

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.add(root);
        while(queue.size() > 0){
    
    
            ArrayList<Integer> temp = new ArrayList<>();
            int size = queue.size();
            for(int i = 0;i < size;i++){
    
    
                TreeNode node = queue.poll();
                temp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            res.add(temp);
        }

        return res;
    }
}

ソードはオファー32-IIIを指します。バイナリツリーを上から下に印刷しますIII

ここに画像の説明を挿入

class Solution {
    
    
    public List<List<Integer>> levelOrder(TreeNode root) {
    
    
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;

        int row = 1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(queue.size() > 0){
    
    
            LinkedList<Integer> temp = new LinkedList<>();
            int size = queue.size();
            for(int i = 0;i < size;i++){
    
    
                TreeNode node = queue.poll();
                if(row % 2 == 0){
    
    
                    temp.addFirst(node.val);
                }else{
    
    
                    temp.add(node.val);
                }
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            row++;
            res.add(temp);
        }

        return res;
    }
}

ソードはオファー34を指します。合計がバイナリツリー内の特定の値であるパス

ここに画像の説明を挿入

class Solution {
    
    
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
    
    
        if(root == null) return res;

        helper(root,sum,new LinkedList<Integer>());
        return res;
    }

    private void helper(TreeNode root,int sum,LinkedList<Integer> path){
    
    
        if(root == null) return;

        path.add(root.val);
        sum -= root.val;
        if(sum == 0 && root.left == null && root.right == null) 
            res.add((LinkedList<Integer>) path.clone());

        helper(root.left,sum,path);
        helper(root.right,sum,path);
        path.removeLast();
    }
}

ソードはオファー54を指します。二分探索木のk番目に大きいノード

ここに画像の説明を挿入

class Solution {
    
    
    int max;
    int k;
    public int kthLargest(TreeNode root, int k) {
    
    
        this.k = k;
        dfs(root);
        return max;
    }

    private void dfs(TreeNode root){
    
    
        if(root == null) return;

        dfs(root.right);
        if(k == 0) return;
        k--;
        if(k == 0) max = root.val;
        dfs(root.left);
    }
}

ソードはオファー55-Iを指します。バイナリツリーの深さ

ここに画像の説明を挿入

class Solution {
    
    
    public int maxDepth(TreeNode root) {
    
    
        if(root == null) return 0;

        int left = maxDepth(root.left);
        int right = maxDepth(root.right);

        return Math.max(left,right) + 1;
    }
}

ソードフィンガーオファー55-II。バランスの取れたバイナリツリー

ここに画像の説明を挿入

class Solution {
    
    
    public boolean isBalanced(TreeNode root) {
    
    
        return dfs(root) != -1;
    }

    private int dfs(TreeNode root){
    
    
        if(root == null) return 0;

        int left = dfs(root.left);
        if(left == -1) return -1;
        int right = dfs(root.right);
        if(right == -1) return -1;

        return Math.abs(left - right) <= 1 ? Math.max(left,right) + 1 : -1;
    }
}

Jian Zhi Offer 68-I。二分探索木の最も近い共通の祖先

ここに画像の説明を挿入

class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(p.val < root.val && q.val < root.val)
            return lowestCommonAncestor(root.left,p,q);
        if(p.val > root.val && q.val > root.val)
            return lowestCommonAncestor(root.right,p,q);

        return root;
    }
}

Jian Zhi Offer 68-II。二分木の最も近い共通の祖先

ここに画像の説明を挿入

class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if(root == null || root == p || root == q) return root;

        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);

        if(left == null && right == null) return null;
        if(left == null) return right;
        if(right == null) return left;
        
        return root;
    }
}

いい加減にして!

おすすめ

転載: blog.csdn.net/qq_46225886/article/details/107591188