# CodeTop每日系列三题------------------2021.11.26

LC543. 二叉树的直径

/**
* 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 diameterOfBinaryTree(TreeNode root) {
return path(root,0);
}

public int path(TreeNode root,int i){
if(root == null)
return 0;
int res = i + 1;
if(root.left == null && root.right == null)
return res;
return path(root.left,res) + path(root.right,res);
}
}


LC234. 回文链表
//存放到数组当中进行一个前后指针遍历

/**
* 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 boolean isPalindrome(ListNode head) {
List<Integer> list = new ArrayList<>();
while (head != null) {
}
int len = list.size();
for (int i = 0; i < len / 2; i++) {
if (!list.get(i).equals(list.get(len - 1- i))) return false;
}
return true;
}
}


LC112. 路径总和

/**
* 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;
if(root.left == null && root.right == null)
return root.val == targetSum;
return hasPathSum(root.left,targetSum - root.val) || hasPathSum(root.right,targetSum - root.val);
}

}


• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

©️2022 CSDN 皮肤主题：1024 设计师：我叫白小胖

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。