513.找树左下角的值
题解及想法
解法一 : 递归法
/**
* 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 deep1 = -1; //深度
private int res;
public int findBottomLeftValue(TreeNode root) {
res = root.val;
findLeftNode(root,0);
return res;
}
private void findLeftNode(TreeNode root,int deep){
if(root == null) return;
if (root.left == null && root.right == null) { //叶子节点
if(deep > deep1){
res = root.val;
deep1 = deep; //更新最大深度
}
}
if (root.left != null) findLeftNode(root.left,deep + 1);
if (root.right != null) findLeftNode(root.right,deep + 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 {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int res = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){//结束条件不能是queue.size(),因为queue.size()一直在变化
TreeNode poll = queue.poll();
if (i == 0) { //层序遍历 保存第一个节点,也就是最左边的节点
res = poll.val;
}
if (poll.left != null) {
queue.offer(poll.left);
}
if (poll.right != null) {
queue.offer(poll.right);
}
}
}
return res;
}
}
112. 路径总和
题解及想法
解法一 : 递归法
/**
* 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) {
if(targetSum == 0){ //返回递归到叶子节点时targetSum是否为0
return true;
}else{
return false;
}
}
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;
}
}
解法二 : 迭代法
/**
* 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;
Stack<TreeNode> stack1 = new Stack<>(); //放每一层的节点
Stack<Integer> stack2 = new Stack<>(); // 放路径总和
stack1.push(root);
stack2.push(root.val);
while(!stack1.isEmpty()){
int size = stack1.size();
for(int i = 0; i < size; i++) {
TreeNode node = stack1.pop();
int sum = stack2.pop();
if(node.left == null && node.right == null && sum == targetSum){
return true;
}
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if(node.right != null){
stack1.push(node.right);
stack2.push(sum + node.right.val);
}
// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if(node.left != null) {
stack1.push(node.left);
stack2.push(sum + node.left.val);
}
}
}
return false;
}
}
106.从中序与后序遍历序列构造二叉树
题解及想法
后序补