补day14:
513.找树左下角的值
深度没想好应该是从上往下算还是从下往上算
- 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
- 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
/**
* 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 {
private int maxDeepth = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
value=root.val;
getValue(root,0);
return value;
}
public void getValue(TreeNode root,int deep){
if(root==null) return;
if(root.left==null&&root.right==null){
if(deep>maxDeepth){
maxDeepth=deep;
value=root.val;
// System.out.println(deep);
return;
}
}
getValue(root.left,deep+1);
getValue(root.right,deep+1);
}
}
654.最大二叉树
考虑应该一个数组是用左闭右闭还是左闭右开考虑了好久。最后是觉得如果出现【6,0,5】这种虽然最大节点是5,但是左闭右闭的节点既有0又有5,没办法判断根节点的右界定啊应该是谁。还是应该用左闭右开的形式,这样判断久类似于数组里面有几个数了。好考虑一点
/**
* 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 constructBinaryTree(nums, 0, nums.length);
}
public TreeNode constructBinaryTree(int[] nums, int left, int right) {
if(right-left<1) return null;
// System.out.println("left"+left);
// System.out.println("right"+right);
if(right-left==1) return new TreeNode(nums[left]);
int maxIndex=left;
int maxValue=nums[left];
for(int i=left+1;i<right;i++){
if(nums[i]>maxValue){
maxIndex=i;
maxValue=nums[i];
}
}
TreeNode root=new TreeNode(maxValue);
root.left=constructBinaryTree(nums,left,maxIndex);
root.right=constructBinaryTree(nums,maxIndex+1,right);
return root;
}
}
617.合并二叉树
完全没有思路,不知道怎么同时遍历两棵树
主要一开始是想把两棵树的节点结合到一颗树root1上,但是如果root1==null,那么久没有办法把root2的节点和过来,如果新增一个treenode,但是不知道上一层的节点,也没办法将两个treenode之间关联起来。
判断节点为null之后,惯性思维的认为直接返回null了。唉
/**
* 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) {
if(root1==null) return root2;
if(root2==null) return root1;
root1.val+=root2.val;
root1.left=mergeTrees(root1.left,root2.left);
root1.right=mergeTrees(root1.right,root2.right);
return root1;
}
}
700.二叉搜索树中的搜索
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root==null) return null;
if(root.val==val) return root;
else{
if(root.val<val) root=searchBST(root.right,val);
else{
root=searchBST(root.left,val);
}
return root;
}
}
}
98.验证二叉搜索树
可以递归中序遍历将二叉搜索树转变成一个数组
/**
* 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 {
TreeNode max;
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
// 左
boolean left = isValidBST(root.left);
if (!left) {
return false;
}
// 中
if (max != null && root.val <= max.val) {
return false;
}
max = root;
// 右
boolean right = isValidBST(root.right);
return right;
}
}
总结:二叉树使用递归,就要想使用前中后哪种遍历方式?(一定先考虑递归的顺序)