今日任务
654.最大二叉树
617.合并二叉树
700.二叉搜索树中的搜索
98.验证二叉搜索树
654.最大二叉树
当不确定一个新的解决方案是否正确的时候
请优先使用暴力解的方式
/** * 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 TreeNode constructMaximumBinaryTree(int[] nums) { return research(0, nums.length, nums); } public TreeNode research(int start, int end, int[] nums){ if (start >= end) return null; int maxs = nums[start]; int index = start; for(int i = start+1; i < end; i++){ if (nums[i] > maxs){ maxs = nums[i]; index = i; } } TreeNode root = new TreeNode(maxs); root.left = research(start, index, nums); root.right = research(index + 1, end, nums); return root; } }
617.合并二叉树
/** * 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 TreeNode mergeTrees(TreeNode root1, TreeNode root2) { return search(root1, root2); } public TreeNode search(TreeNode root1, TreeNode root2){ if(root1 == null && root2 == null) return null; if(root1 == null){ return root2; } if(root2 == null){ return root1; } TreeNode temp = new TreeNode(root1.val + root2.val); temp.left = search(root1.left, root2.left); temp.right = search(root1.right, root2.right); return temp; } }
700.二叉搜索树中的搜索
二叉搜索树是一个有序树:
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉搜索树
/** * 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 TreeNode searchBST(TreeNode root, int val) { while(root != null && root.val != val){ // System.out.println(root.val); if (root.val > val) root = root.left; else root = root.right; } return root; } }
98.验证二叉搜索树
遇到 搜索树,一定想着中序遍历,这样才能利用上特性。
要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。
有了这个特性,验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了
- 陷阱1
不能单纯的比较左节点小于中间节点,右节点大于中间节点就完事了。
- 陷阱2
样例中最小节点 可能是int的最小值,如果这样使用最小的int来比较也是不行的。
可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇二叉树:听说递归能做的,栈也能做! (opens new window),二叉树:前中后序迭代方式统一写法(opens new window)
/** * 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 { Queue<Integer> list = new LinkedList<Integer> (); public boolean isValidBST(TreeNode root) { search(root); int n = list.size(); int temp = list.poll(); while(n > 1){ int temp2 = list.poll(); if(temp2 <= temp) return false; temp = temp2; n--; } return true; } public void search(TreeNode node){ if (node == null) return; search(node.left); list.offer(node.val); System.out.println(node.val); search(node.right); } }