/**
* 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 maxDepth(TreeNode root) {
if(root == null){
return 0;
}
//使用递归
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
2. 平衡树
/**
* 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 {
boolean result = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
}
//得到二叉树的高度
public int maxDepth(TreeNode root){
if(root == null){
return 0;
}
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if(Math.abs(l - r) > 1) result = false;
return Math.max(l, r) + 1;
}
}
3. 两节点的最长路径
/**
* 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 {
int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
maxDepth(root);
return max;
}
public int maxDepth(TreeNode root){
if(root == null) return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
// if((l + r) > max) max = (l + r);此处代码可以优化
max = Math.max((l + r), max);
return Math.max(l, r) + 1;
}
}
4. 翻转树
/**
* 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 TreeNode invertTree(TreeNode root) {
if(root == null){
return null;
}
TreeNode left = root.left;
root.left = invertTree(root.right);
root.right = invertTree(left);
return root;
}
}
5. 归并两棵树
/**
* 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 TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
TreeNode t1 = root1;
TreeNode t2 = root2;
if(t1 == null && t2 ==null) return null;
if(t1 == null) return t2;
if(t2 == null) return t1;
TreeNode root = new TreeNode(t1.val + t2.val);
root.left = mergeTrees(t1.left, t2.left);
root.right = mergeTrees(t1.right, t2.right);
return root;
}
}
6. 判断路径和是否等于一个数
/**
* 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 && root.val == targetSum) return true;
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}
}
7. 统计路径和等于一个数的路径数量
/**
* Definition for a binary tree node.
* public class TreeNode {
* long val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(long val) { this.val = val; }
* TreeNode(long val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int pathSum(TreeNode root, int targetSum) {
if(root == null) return 0;
int ret = pathSumStartWithRoot(root,targetSum) + pathSum(root.left,targetSum) + pathSum(root.right, targetSum);//后边两个相当于将节点向下移动。重新找根节点,太妙了
return ret;
}
//先写一个路径方法,从根节点开始的。
public int pathSumStartWithRoot(TreeNode root, int sum){
if(root == null) return 0;
int ret = 0;
if(root.val == sum) ret++;
ret += pathSumStartWithRoot(root.left,sum - root.val) + pathSumStartWithRoot(root.right, sum - root.val);
return ret;
}
}
8. 子树
/**
* 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 isSubtree(TreeNode root, TreeNode subRoot) {
if(root == null) return false;
return isSubtreeWithRoot(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
public boolean isSubtreeWithRoot(TreeNode s, TreeNode t){
if(t == null && s == null) return true;
if(t == null || s == null) return false;
if(s.val != t.val) return false;
return isSubtreeWithRoot(s.left,t.left) && isSubtreeWithRoot(s.right, t.right);
}
}
9. 树的对称
/**
* 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 isSymmetric(TreeNode root) {
return isStartWithRoot(root.left, root.right);
}
public boolean isStartWithRoot(TreeNode root, TreeNode subroot){//注:这里可以用是相同的函数名,构成方法的重载。
if(root == null && subroot == null) return true;
if(root == null || subroot == null) return false;
if(root.val != subroot.val) return false;
return isStartWithRoot(root.right, subroot.left) && isStartWithRoot(root.left, subroot.right);
}
}
10. 最小路径
/**
* 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 minDepth(TreeNode root) {
if(root == null) return 0;
int l = minDepth(root.left);
int r = minDepth(root.right);
if(l == 0 || r == 0) return l + r + 1;//这个地方还是挺不好理解的。。。因为要回溯,就是这个节点的左右子节点有一个为空,那么就会返回值,代表当前这个节点的最大深度。
return Math.min(l, r) + 1;
}
}