第六章 二叉树 part06

58 阅读2分钟

654. Maximum Binary Tree

You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:

  1. Create a root node whose value is the maximum value in nums.
  2. Recursively build the left subtree on the subarray prefix to the left of the maximum value.
  3. Recursively build the right subtree on the subarray suffix to the right of the maximum value.

Return the maximum binary tree built from nums.

题目解析:

  • 使用迭代构建二叉树

代码:

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return construct(nums, 0, nums.length - 1);
    }

    public TreeNode construct(int[] nums, int left, int right) {
        if (left > right) return null;
        int maxVal = nums[left], maxIdx = left;
        for (int i = left; i <= right; i++) {
            if (nums[i] > maxVal) {
                maxIdx = i;
                maxVal = nums[i];
            }
        }
        TreeNode root = new TreeNode(maxVal);
        root.left = construct(nums, left, maxIdx - 1);
        root.right = construct(nums, maxIdx + 1, right);
        return root;
    }
}

617. Merge Two Binary Trees

You are given two binary trees root1 and root2.

Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.

Return the merged tree.

Note:  The merging process must start from the root nodes of both trees.

题目解析:

  • 递归

代码:

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) return root2;
        if (root2 == null) return root1;
        TreeNode root = new TreeNode(root1.val + root2.val);
        root.left = mergeTrees(root1.left, root2.left);
        root.right = mergeTrees(root1.right, root2.right);
        return root;
    }
}

700. Search in a Binary Search Tree

You are given the root of a binary search tree (BST) and an integer val.

Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

代码:

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null) return root;
        if (root.val < val) {
            return searchBST(root.right, val);
        } else if (root.val > val) {
            return searchBST(root.left, val);
        }
        return root;
    }
}

98. Validate Binary Search Tree

Given the root of a binary tree, determine if it is a valid binary search tree (BST) .

valid BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.

  • The right subtree of a node contains only nodes with keys greater than the node's key.

  • Both the left and right subtrees must also be binary search trees.

题目解析:

  • 第一种:根据二叉搜索树的性质,左边 < 中间 < 右边,所以可以按照中序遍历前一个元素小于后一个元素,需要定义全局边量来代表前一个元素
  • 第二种:就是保证左边最大值小于root,并且右边最小值小于root即可

代码: 1. 第一种

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

2. 第二种

class Solution {
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        TreeNode left = root.left, right = root.right;
        long leftMax = Long.MIN_VALUE, rightMin = Long.MAX_VALUE;
        while(left != null) {
            leftMax = left.val;
            left = left.right;

        }
        while(right != null) {
            rightMin = right.val;
            right = right.left;
        }
        if (leftMax >= root.val || rightMin <= root.val) {
            return false;
        }
        return isValidBST(root.left) && isValidBST(root.right);
    }

}