2024/8/28 LeetCode Hot100 binaryTree Num543有bug 106案例过101

This commit is contained in:
Cool 2024-08-29 00:05:08 +08:00
parent 31c3f9c0c1
commit 4dbf7ab57e
6 changed files with 187 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package com.cool.hot100.binary_tree;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/23:23
* DayNumber 4
* Hard 1
* Level 3
*/
public class Num101 {
public boolean isSymmetric(TreeNode root) {
if (root == null) return false;
if (root.left != null && root.right != null) {
if (root.left.val != root.right.val) return false;
else {
return isSymmetric(root.left, root.right);
}
} else return root.left == root.right;
}
public boolean isSymmetric(TreeNode left, TreeNode right) {
if (left == right) return true;
if (left != null && right != null) {
boolean res = (left.val == right.val);
if (res) res = isSymmetric(left.left, right.right);
if (res) res = isSymmetric(left.right, right.left);
return res;
}
return false;
}
}

View File

@ -0,0 +1,38 @@
package com.cool.hot100.binary_tree;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/23:12
* DayNumber 2
* Hard 1
* Level 3
*/
public class Num104 {
private int max=0;
private int cur=0;
public int maxDepth(TreeNode root) {
if(root==null) return max;
dfs(root);
return max;
}
private void dfs(TreeNode root){
if(root==null)return;
if(root.left!=null) {
cur++;
max=Math.max(max,cur);
dfs(root.left);
cur--;
}
if(root.right!=null){
cur++;
max=Math.max(max,cur);
dfs(root.right);
cur--;
}
}
}

View File

@ -0,0 +1,28 @@
package com.cool.hot100.binary_tree;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/23:20
* DayNumber 3
* Hard 1
* Level 3
*/
public class Num226 {
public TreeNode invertTree(TreeNode root) {
dfsInvertTree(root);
return root;
}
private void dfsInvertTree(TreeNode root) {
if (root == null) return;
TreeNode right = root.right;
root.right=root.left;
root.left=right;
dfsInvertTree(root.left);
dfsInvertTree(root.right);
}
}

View File

@ -0,0 +1,28 @@
package com.cool.hot100.binary_tree;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/23:52
* DayNumber 5
* Hard 1
* Level 3
*/
public class Num543 {
public int diameterOfBinaryTree(TreeNode root) {
if (root == null) return 0;
int left = getTreeDeep(root.left);
int right = getTreeDeep(root.right);
return left + right;
}
private int getTreeDeep(TreeNode root) {
if (root == null) return 0;
int res = getTreeDeep(root.left);
res = Math.max(res, getTreeDeep(root.right));
return res + 1;
}
}

View File

@ -0,0 +1,31 @@
package com.cool.hot100.binary_tree;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/23:02
* DayNumber 1
* Hard 1
* Level 3
*/
public class Num94 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
mediumTraverse(root,list);
return list;
}
private void mediumTraverse(TreeNode root,List<Integer> list){
if(root==null){
return;
}
mediumTraverse(root.left,list);
list.add(root.val);
mediumTraverse(root.right,list);
}
}

View File

@ -0,0 +1,27 @@
package com.cool.hot100.binary_tree;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/23:03
* @Description: 二叉树节点
*/
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;
}
}