力扣
class Solution {
TreeNode prev;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
boolean L = isValidBST(root.left);
if (prev != null && prev.val >= root.val) return false;
prev = root;
boolean R = isValidBST(root.right);
return L && R;
}
}
力扣
class Solution {
int maxValue;
public int maxPathSum(TreeNode root) {
maxValue = Integer.MIN_VALUE;
maxPathDown(root);
return maxValue;
}
private int maxPathDown(TreeNode root) {
if (root == null) return 0;
int left = Math.max(0,maxPathDown(root.left));
int right = Math.max(0,maxPathDown(root.right));
maxValue = Math.max(maxValue, left + right + root.val);
return Math.max(left, right) + root.val;
}
}
力扣
class Solution {
int res = 0;
public int sumNumbers(TreeNode root) {
if (root == null) return 0;
dfs(root,new ArrayList<>());
return res;
}
private void dfs(TreeNode root, List<Integer> row) {
row.add(root.val);
if (root.left != null) dfs(root.left, row);
if (root.right != null) dfs(root.right, row);
if (root.left == null && root.right == null) {
int sum = 0;
for (int num : row) {
sum = sum * 10 + num;
}
res += sum;
}
row.remove(row.size() - 1);
}
}
力扣
class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root==null||root.left==null)
return root;
TreeNode newRoot=upsideDownBinaryTree(root.left);
TreeNode rRoot=newRoot;
while(rRoot.right!=null)
rRoot=rRoot.right;
rRoot.left=root.right;
rRoot.right=new TreeNode(root.val);
return newRoot;
}
}
力扣
class Solution {
int ans = 1;
public int diameterOfBinaryTree(TreeNode root) {
depth(root);
return ans - 1;
}
public int depth(TreeNode root) {
if (root == null) return 0;
int L = depth(root.left);
int R = depth(root.right);
ans = Math.max(ans, L + R + 1);
return Math.max(L, R) + 1;
}
}
力扣
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return createTree(nums, 0, nums.length - 1);
}
private TreeNode createTree(int[] nums, int begin, int end) {
if (begin > end) return null;
int mid = (begin + end) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = createTree(nums, begin, mid - 1);
root.right = createTree(nums, mid + 1, end);
return root;
}
}
力扣
public ListNode[] listOfDepth(TreeNode tree) {
if (tree == null) return new ListNode[0];
List<ListNode> res = new ArrayList<>();
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(tree);
ListNode prev = new ListNode(0);
while (!queue.isEmpty()) {
int size = queue.size();
ListNode cur = prev;
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
cur.next = new ListNode(node.val);
cur = cur.next;
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
res.add(prev.next);
prev.next = null;
}
return res.toArray(new ListNode[0]);
}
力扣
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
if (Math.abs(depth(root.left) - depth(root.right)) > 1) return false;
else return isBalanced(root.left) && isBalanced(root.right);
}
private int depth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(depth(root.left), depth(root.right));
}
}
力扣
class Solution {
TreeNode prev;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
boolean L = isValidBST(root.left);
if (prev != null && prev.val >= root.val) return false;
prev = root;
boolean R = isValidBST(root.right);
return L && R;
}
}
力扣
class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if (root == null || p == null) return null;
if (root.val <= p.val) {
return inorderSuccessor(root.right, p);
} else {
TreeNode left = inorderSuccessor(root.left, p);
return (left != null) ? left : root;
}
}
}
力扣
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) return root;
if (root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) return root;
if (left != null) return left;
else return right;
}
力扣
class Solution {
public List<List<Integer>> BSTSequences(TreeNode root) {
List<TreeNode> items = new ArrayList<>();
if (root != null) items.add(root);
List<Integer> tempRes = new ArrayList<>();
List<List<Integer>> res = new ArrayList<>();
dfs(items, tempRes, res);
return res;
}
private void dfs(List<TreeNode> items, List<Integer> tempRes, List<List<Integer>> res) {
if (items.isEmpty()) {
res.add(new ArrayList<>(tempRes));
} else {
int len = items.size();
for (int i = 0; i < len; i++) {
TreeNode temp = items.get(i);
items.remove(i);
if (temp.left != null) items.add(temp.left);
if (temp.right != null) items.add(temp.right);
tempRes.add(temp.val);
dfs(items, tempRes, res);
tempRes.remove(tempRes.size() - 1);
items.add(i, temp);
items.remove(temp.left);
items.remove(temp.right);
}
}
}
}
力扣
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
else if (t1 == null || t2 == null) return false;
if (t1.val == t2.val) {
boolean left = checkSubTree(t1.left, t2.left);
boolean right = checkSubTree(t1.right, t2.right);
return left && right;
} else {
boolean left = checkSubTree(t1.left, t2);
boolean right = checkSubTree(t1.right, t2);
return left || right;
}
}
力扣
class Solution {
int res = 0;
public int pathSum(TreeNode root, int sum) {
int dep = depth(root);
int[] paths = new int[dep];
pathSum(root, sum, 0, paths);
return res;
}
public void pathSum(TreeNode root, int sum, int level, int[] paths) {
if (root == null) return;
paths[level] = root.val;
int t = 0;
for (int i = level; i >= 0; i--) {
t += paths[i];
if (t == sum) res++;
}
pathSum(root.left, sum, level + 1, paths);
pathSum(root.right, sum, level + 1, paths);
}
public int depth(TreeNode root) {
if (root == null) return 0;
return Math.max(depth(root.left), depth(root.right)) + 1;
}
}
力扣
class Solution {
TreeNode head = null, curr = null;
public TreeNode convertBiNode(TreeNode root) {
if (root == null) return root;
convertBiNode(root.left);
if (head == null) {
head = root;
} else {
curr.right = root;
}
curr = root;
root.left = null;
convertBiNode(root.right);
return head;
}
}
利用中序遍历来实现
力扣
public TreeNode mirrorTree(TreeNode root) {
if (root == null) return null;
TreeNode temp = root.right;
root.right = mirrorTree(root.left);
root.left = mirrorTree(temp);
return root;
}
力扣
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> row = new ArrayList<>();
if (root != null) dfs(res,row,root, sum);
return res;
}
private void dfs(List<List<Integer>> res, List<Integer> row, TreeNode root, int sum) {
row.add(root.val);
if (root.left != null) dfs(res,row,root.left,sum - root.val);
if (root.right != null) dfs(res,row,root.right,sum - root.val);
if (root.left == null && root.right == null) {
if (sum == root.val) {
res.add(new ArrayList<>(row));
}
}
row.remove(row.size() - 1);
}
}
力扣
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
if (Math.abs(depth(root.left) - depth(root.right)) > 1) return false;
return isBalanced(root.left) && isBalanced(root.right);
}
public int depth(TreeNode root) {
if (root == null) return 0;
return Math.max(depth(root.left),depth(root.right)) + 1;
}
}