LeetCode:513. 找树左下角的值 - 力扣(LeetCode)
1.思路
定义两个变量,一个标记最大深度,一个记录对应深度的值。采用前序遍历即可。
2.代码实现
/**
* 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 {
int Deep = -1;
int value = 0;
public int findBottomLeftValue(TreeNode root) {
value = root.val;
findLeftValue(root, 0);
return value;
}
private void findLeftValue (TreeNode root, int deep) {
if (root == null) return;
if (root.left == null && root.right == null) {
if (deep > Deep) {
value = root.val;
Deep = deep;
}
}
if (root.left != null) findLeftValue(root.left, deep + 1);
if (root.right != null) findLeftValue(root.right, deep + 1);
}
}
3.复杂度分析
时间复杂度:O(n).
空间复杂度:O(n).
LeetCode:
1.思路
前序遍历,递归调用函数即可
2.代码实现
/**
* 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 boolean hasPathSum(TreeNode root, int targetSum) {
// 前序遍历
if (root == null) return false;
targetSum -= root.val;
if (root.left == null && root.right == null) {
return targetSum == 0;
}
if (root.left != null) {
boolean left = hasPathSum(root.left, targetSum);
if (left) {
return true;
}
}
if (root.right != null) {
boolean right = hasPathSum(root.right, targetSum);
if (right) {
return true;
}
}
return false;
}
}
3.复杂度分析
时间复杂度:O(n).
空间复杂度:O(n).
LeetCode:113. 路径总和 II - 力扣(LeetCode)
1.思路
前序遍历,对当前节点进行判空处理,不为空就将当前节点值加入到路径中,并与目标值进行比较,遍历到叶子节点对被减的目标值进行判断,当targetSum==0时,表明该路径满足条件,将该路径加入到结果集中。
2.代码实现
/**
* 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 List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
List<Integer> path = new LinkedList<>();
preorderdfs(root, targetSum, res, path);
return res;
}
void preorderdfs(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
path.add(root.val);
if (root.left == null && root.right == null) {
if (targetSum - root.val == 0) {
res.add(new ArrayList<>(path));
}
return;
}
if (root.left != null) {
preorderdfs(root.left, targetSum - root.val, res, path);
path.remove(path.size() - 1);
}
if (root.right != null) {
preorderdfs(root.right, targetSum - root.val, res, path);
path.remove(path.size() - 1);
}
}
}
3.复杂度分析
时间复杂度:O(n^2).
空间复杂度:O(n).
LeetCode:
1.思路
借助前序结果数组和后序结果数组的特性,后序数组的最后一个数字为根节点,用来获取中序数组的索引并切割中序数组,得到左右数组,借助中序切割后的左右数组大小切割后序数组,如此递归循环。
2.代码实现
/**
* 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 (postorder.length == 0 || inorder.length == 0) {
return null;
}
return buildHelper(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode buildHelper(int[] inorder, int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) {
// 终止条件
if (postorderStart == postorderEnd) {
return null;
}
int rootVal = postorder[postorderEnd - 1];
TreeNode root = new TreeNode(rootVal);
int middleIndex;
// 遍历中序数组,找到根节点的索引
for (middleIndex = inorderStart; middleIndex < inorderEnd; middleIndex++) {
if (inorder[middleIndex] == rootVal)
break;
}
// 切割中序数组,分为左右数组
int leftInorderStart = inorderStart;
int leftInorderEnd = middleIndex;
int rightInorderStart = middleIndex + 1;
int rightInorderEnd = inorderEnd;
// 切割后序数组,分为后序左右子数组
int leftPostorderStart = postorderStart;
int leftPostorderEnd = postorderStart + (middleIndex - inorderStart);
int rightPostorderStart = leftInorderEnd;
int rightPostorderEnd = postorderEnd - 1;
// 左
root.left = buildHelper(inorder, leftInorderStart, leftInorderEnd, postorder, leftPostorderStart, leftPostorderEnd);
// 右
root.right = buildHelper(inorder, rightInorderStart, rightInorderEnd, postorder, postorderStart, postorderEnd);
return root;
}
}
3.复杂度分析
时间复杂度:O(n).
空间复杂度:O(n).