Leetcode算法题-二叉树

98. 验证二叉搜索树

力扣

class Solution {
    TreeNode prev;
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        boolean L = isValidBST(root.left);
        if (prev != null && prev.val >= root.val) return false;
        prev = root;
        boolean R = isValidBST(root.right);
        return L && R;
    }
}

124. 二叉树中的最大路径和

力扣

class Solution {
    int maxValue;
    public int maxPathSum(TreeNode root) {
        maxValue = Integer.MIN_VALUE;
        maxPathDown(root);
        return maxValue;
    }
    
    private int maxPathDown(TreeNode root) {
        if (root == null) return 0;
        int left = Math.max(0,maxPathDown(root.left));
        int right = Math.max(0,maxPathDown(root.right));
        maxValue = Math.max(maxValue, left + right + root.val);
        return Math.max(left, right) + root.val;
    }
}

129. 求根到叶子节点数字之和

力扣

class Solution {
    int res = 0;
    public int sumNumbers(TreeNode root) {
        if (root == null) return 0;
        dfs(root,new ArrayList<>());
        return res;
    }
    private void dfs(TreeNode root, List<Integer> row) {
        row.add(root.val);
        if (root.left != null) dfs(root.left, row);
        if (root.right != null) dfs(root.right, row);
        if (root.left == null && root.right == null) {
            int sum = 0;
            for  (int num : row) {
                sum = sum * 10 + num;
            }
            res += sum;
        }
        row.remove(row.size() - 1);
    }
}

156. 上下翻转二叉树

力扣

class Solution {
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if(root==null||root.left==null)
            return root;
        TreeNode newRoot=upsideDownBinaryTree(root.left);
        TreeNode rRoot=newRoot;
        while(rRoot.right!=null)
            rRoot=rRoot.right;
        rRoot.left=root.right;
        rRoot.right=new TreeNode(root.val);
        return newRoot;
    }
}

1325. 删除给定值的叶子节点

543. 二叉树的直径

力扣

class Solution {
    int ans = 1;
    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return ans - 1;
    }

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

面试题 04.02. 最小高度树

力扣

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return createTree(nums, 0, nums.length - 1);    
    }
    
    private TreeNode createTree(int[] nums, int begin, int end) {
        if (begin > end) return null;
        int mid = (begin + end) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = createTree(nums, begin, mid - 1);
        root.right = createTree(nums, mid + 1, end);
        return root;
    }
}

面试题 04.03. 特定深度节点链表

力扣

public ListNode[] listOfDepth(TreeNode tree) {
        if (tree == null) return new ListNode[0];
        List<ListNode> res = new ArrayList<>();
        Deque<TreeNode> queue = new ArrayDeque<>();
        queue.offer(tree);
        ListNode prev = new ListNode(0);
        while (!queue.isEmpty()) {
            int size = queue.size();
            ListNode cur = prev;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                cur.next = new ListNode(node.val);
                cur = cur.next;
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            res.add(prev.next);
            prev.next = null;
        }
        return res.toArray(new ListNode[0]);
    }

面试题 04.04. 检查平衡性

力扣

class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        if (Math.abs(depth(root.left) - depth(root.right)) > 1) return false;
        else return isBalanced(root.left) && isBalanced(root.right);
    }
    
    private int depth(TreeNode root) {
        if (root == null) return 0;
        return 1 + Math.max(depth(root.left), depth(root.right));
    }
}

面试题 04.05. 合法二叉搜索树

力扣

class Solution {
    TreeNode prev;
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        boolean L = isValidBST(root.left);
        if (prev != null && prev.val >= root.val) return false;
        prev = root;
        boolean R = isValidBST(root.right);
        return L && R;
    }
}

面试题 04.06. 后继者

力扣

class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (root == null || p == null) return null;
        if (root.val <= p.val) {
            return inorderSuccessor(root.right, p);
        } else {
            TreeNode left = inorderSuccessor(root.left, p);
            return (left != null) ? left : root;
        }
    }
}

面试题 04.08. 首个共同祖先

力扣

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return root;
        if (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 root;
        if (left != null) return left;
        else return right;
    }

面试题 04.09. 二叉搜索树序列

力扣

class Solution {
    public List<List<Integer>> BSTSequences(TreeNode root) {
        List<TreeNode> items = new ArrayList<>();
        if (root != null) items.add(root);
        List<Integer> tempRes = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        dfs(items, tempRes, res);
        return res;
    }

    private void dfs(List<TreeNode> items, List<Integer> tempRes, List<List<Integer>> res) {
        if (items.isEmpty()) {
            res.add(new ArrayList<>(tempRes));
        } else {
            int len = items.size();
            for (int i = 0; i < len; i++) {
                TreeNode temp = items.get(i);
                items.remove(i);
                if (temp.left != null) items.add(temp.left);
                if (temp.right != null) items.add(temp.right);
                tempRes.add(temp.val);
                dfs(items, tempRes, res);
                tempRes.remove(tempRes.size() - 1);
                items.add(i, temp);
                items.remove(temp.left);
                items.remove(temp.right);
            }
        }
    }
}

面试题 04.10. 检查子树

力扣

public boolean checkSubTree(TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null) return true;
        else if (t1 == null || t2 == null) return false;
        if (t1.val == t2.val) {
            boolean left = checkSubTree(t1.left, t2.left);
            boolean right = checkSubTree(t1.right, t2.right);
            return left && right;
        } else {
            boolean left = checkSubTree(t1.left, t2);
            boolean right = checkSubTree(t1.right, t2);
            return left || right;
        }
    }

面试题 04.12. 求和路径

力扣

class Solution {
    int res = 0;
    public int pathSum(TreeNode root, int sum) {
        int dep = depth(root);
        int[] paths = new int[dep];
        pathSum(root, sum, 0, paths);
        return res;
    }

    public void pathSum(TreeNode root, int sum, int level, int[] paths) {
        if (root == null) return;
        paths[level] = root.val;
        int t = 0;
        for (int i = level; i >= 0; i--) {
            t += paths[i];
            if (t == sum) res++;
        }
        pathSum(root.left, sum, level + 1, paths);
        pathSum(root.right, sum, level + 1, paths);
    }

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

面试题 17.12. BiNode

力扣

class Solution {
    TreeNode head = null, curr = null;

    public TreeNode convertBiNode(TreeNode root) {
        if (root == null) return root;
        convertBiNode(root.left);
        if (head == null) {
            head = root;
        } else {
            curr.right = root;
        }
        curr = root;
        root.left = null;
        convertBiNode(root.right);
        return head;
    }
}

利用中序遍历来实现

剑指 Offer 27. 二叉树的镜像

力扣

public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null;
        TreeNode temp = root.right;
        root.right = mirrorTree(root.left);
        root.left = mirrorTree(temp);
        return root;
    }

剑指 Offer 34. 二叉树中和为某一值的路径

力扣

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> row = new ArrayList<>();
        if (root != null) dfs(res,row,root, sum);
        return res;
    }
    
    private void dfs(List<List<Integer>> res, List<Integer> row, TreeNode root, int sum) {
        row.add(root.val);
        if (root.left != null) dfs(res,row,root.left,sum - root.val);
        if (root.right != null) dfs(res,row,root.right,sum - root.val);
        if (root.left == null && root.right == null) {
            if (sum == root.val) {
                res.add(new ArrayList<>(row));
            }
        }
        row.remove(row.size() - 1);
    }
}

剑指 Offer 55 - II. 平衡二叉树

力扣

class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        if (Math.abs(depth(root.left) - depth(root.right)) > 1) return false;
        return isBalanced(root.left) && isBalanced(root.right);
    }
    
    public int depth(TreeNode root) {
        if (root == null) return 0;
        return Math.max(depth(root.left),depth(root.right)) + 1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值