9.27 lc
总结 : 继续是 二叉树的 遍历~~ 递归开始有点感觉了
从一开始的 连遍历都迷糊。现在 好多了:
试着自己去分析题: 开心!
21. 合并两个有序链表 - 力扣(LeetCode) (leetcode-cn.com)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//谁空 返回另一个
if(l1 == null) return l2;
if(l2 == null) return l1;
// 一开始 想错了 :
//
// 迭代这种题: 就应该 设置一个 哨兵节点 类似于 交换里的 中间量 来串起来两个 二叉树;
if(l1.val >l2.val){
l2.next =mergeTwoLists(l2.next,l1);
return l2;
}else{
l1.next =mergeTwoLists(l1.next,l2);
return l1;
}
}
}
222. 完全二叉树的节点个数 - 力扣(LeetCode) (leetcode-cn.com)
:一开始 很快呀 上来就是个 bfs 直接长度完事~~
可这道题 看的 是 完全二叉树的问题:
- 明白两个词
- 树的深度 : 是自上而下的 从根节点到叶子的个数
- 树的高度: 是自下而上的 从 叶子到根节点的个数
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int countNodes(TreeNode root) {
if(root == null){
return 0;
}
//左树 右树 深度
int left = countLevel(root.left);
int right = countLevel(root.right);
//左右深度 相同 说明 左子树满了 直接 打印+递归 右就可以了
if(left == right){
return countNodes(root.right) + (1<<left);
}else{
//相反, 右子树比左子树 低了。 打印 右子树的 + 递归左子树
return countNodes(root.left) + (1<<right);
}
}
//计算 树的深度
private int countLevel(TreeNode root){
int level = 0;
while(root != null){
level++;
root = root.left;
}
return level;
}
}
110. 平衡二叉树 - 力扣(LeetCode) (leetcode-cn.com)
:bst: 左右子树高度相差 不超过 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 boolean isBalanced(TreeNode root) {
return height(root) >=0;
}
public int height(TreeNode root){
if(root == null) return 0;
//递归
int l=height(root.left);
int r=height(root.right);
if(l>=0 && r>=0 && Math.abs(l-r)<=1){
return Math.max(l,r)+1;
}else{
return -1;
}
}
}
257. 二叉树的所有路径 - 力扣(LeetCode) (leetcode-cn.com)
:难度 在于 对于 String 字串的 使用 不好…
没整除 输出要的格式 ~ 。~;
/**
* 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<String> binaryTreePaths(TreeNode root) {
List<String> set=new ArrayList<>();
if(root == null) return set;
s(root,set,"");
return set;
}
//root :根节点 res 结果集 cur 结果字符串;
public void s(TreeNode root , List<String> res,String cur){
if(root == null) return ;
cur+=root.val;
if(root.left == null && root.right == null){
res.add(cur);
}else{
s(root.left,res,cur+"->");
s(root.right,res,cur+"->");
}
}
}
404. 左叶子之和 - 力扣(LeetCode) (leetcode-cn.com)
: 这个递归 很好玩~ 但是 我用它解决类似的 是错的~~
/**
* 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 sumOfLeftLeaves(TreeNode root) {
if(root == null) return 0;
return s(root);
}
public int s(TreeNode root){
if(root == null) return 0;
int res =0;
//这里给出的 符合这样树的条件:
if(root.left != null && root.left.left == null && root.left.right == null){
res += root.left.val;
}
return s(root.left)+s(root.right)+res;
}
}
513. 找树左下角的值 - 力扣(LeetCode) (leetcode-cn.com)
: 没写出来 … 菜鸡本鸡
: 下面是错误的代码…
/**
* 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) {
if(root == null) return 0;
Integer res =Integer.MAX_VALUE;
if( root.left != null && root.left.left== null && root.left.right == null ){
res =Math.min(res,root.left.val);
}
return Math.min(findBottomLeftValue(root.left),findBottomLeftValue(root.right));
}
}