513. 找树左下角的值
这里有一个点:树左下角的值是指最后一层最左边的节点。所以这道题跟深度有关
class Solution {
int maxDepth = 0;
int result ;
public int findBottomLeftValue(TreeNode root) {
//要记录深度数据,另外写一个递归方法
if (root.left == null && root.right == null) return root.val;
find(root, 1);
return result;
}
public void find(TreeNode root, int depth) {
if (root != null && root.left == null && root.right == null) {
if (depth > maxDepth) {
maxDepth = depth;
result = root.val;
} else {
return ;
}
}
if (root.left != null) find(root.left, depth + 1);
if (root.right != null) find(root.right, depth + 1);
}
}
112. 路径总和
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
boolean left = false;
boolean right = false;
if (root == null) return false;
if (root.val == targetSum && root.left == null && root.right == null) return true;
if (root.left != null) {
left = hasPathSum(root.left, targetSum - root.val);
}
if (root.right != null) {
right = hasPathSum(root.right, targetSum - root.val);
}
return left||right;
}
}
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) {
TreeNode tree = constuct(nums, 0, nums.length - 1);
return tree;
}
public TreeNode constuct(int[] nums, int low, int high) {
if (low > high) return null;
int value = Integer.MIN_VALUE;
int p = 0;
for (int i = low; i <= high; i++) {
if (value < nums[i]) {
value = nums[i];
p = i;
}
}
TreeNode root = new TreeNode(value);
root.left = constuct(nums, low, p - 1);
root.right = constuct(nums, p + 1, high);
return root;
}
}
105.从前序和中序遍历构造二叉树
前序的特点是根节点在第一个,中序的特点是根节点在中间,将数组分为了左子树和右子树。因此想法是先通过前序找到根节点,然后将中序遍历数组分为左子树和右子树,然后迭代,从左子树中找到根节点。。。难点在于确定前序遍历中左子树和右子树的左右边界,想不出来的时候就画图
/**
* 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 buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 1) return new TreeNode(preorder[0]);
TreeNode root = construct(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
return root;
}
public TreeNode construct(int[] preorder, int[] inorder, int prestart, int preEnd, int inStart, int inEnd) {
if (prestart > preEnd || inStart > inEnd) return null;
int rootValue = preorder[prestart];
TreeNode root = new TreeNode(rootValue);
int p = 0;
for (int i = inStart; i <= inEnd; i ++) {
if (inorder[i] == rootValue) {
p = i;
break;
}
}
int leftSize= p - inStart;
root.left = construct(preorder, inorder, prestart + 1, prestart + leftSize, inStart, p - 1);
root.right = construct(preorder, inorder, prestart + leftSize + 1, preEnd, p + 1, inEnd);
return root;
}
}
106.从中序和后续遍历构造二叉树
中序遍历和后续遍历的特点是,后序遍历的最后一个值是根节点,其余与前一道相似。难点依然是找左右子树的边界值
/**
* 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 buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 1) return new TreeNode(inorder[0]);
TreeNode root = construct(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
return root;
}
public TreeNode construct(int[] inorder, int[] postorder, int inStart, int inEnd, int postStart, int postEnd) {
if (inStart > inEnd || postStart > postEnd) return null;
int rootValue = postorder[postEnd];
TreeNode root = new TreeNode(rootValue);
int p = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootValue) {
p = i;
break;
}
}
int leftSize = p - inStart;
root.left = construct(inorder, postorder, inStart, p - 1, postStart, postStart + leftSize - 1);
root.right = construct(inorder, postorder, p + 1, inEnd, postStart + leftSize, postEnd - 1);
return root;
}
}
day 16撒花~今天提离职了,该回去准备秋招了!