算法通关村第八关|青铜|二叉树的经典算法题

1.判断两棵树是否相同

public boolean isSameTree(TreeNode p, TreeNode q) {
	if (p == null && q == null) {
        return true;
    }
    if (p == null || q == null) {
        return false;
    }
    if (p.val != q.val) {
        return false;
    }
    return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}

2.一棵对称二叉树

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return check(root.left, root.right);
    }

    public boolean check(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        return check(p.left, q.right) && check(p.right, q.left);
    }
}

3.合并二叉树

public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
	if (t1 == null) {
        return t2;
    }
    if (t2 == null) {
        return t1;
    }
    TreeNode merged = new TreeNode(t1.val + t2.val);
    merged.left = mergeTrees(t1.left, t2.left);
    merged.right = mergeTrees(t1.right, t2.right);
    return merged;
}

4.一对对称二叉树

public boolean isSymmetric(TreeNode p, TreeNode q) {
	if (p == null && q == null) {
        return true;
    }
    if (p == null || q == null) {
        return false;
    }
    if (p.val != q.val) {
        return false;
    }
    return isSymmetric(p.left, q.right) && isSymmetric(p.right ,q.left);
}

5.二叉树的所有路径

public List<String> binaryTreePaths(TreeNode root) {
	List<String> res = new ArrayList<>();
	dfs(root, "", res);
	return res;
}

void dfs(TreeNode root, String path, List<String> res) {
	if (root == null) {
        return;
    }
    if (root.left == null && root.right == null) {
        res.add(path + root.val);
        return;
    }
    dfs(root.left, path + root.val + "->", res);
    dfs(root.right, path + root.val + "->", res);
}

6.路径总和

public boolean hasPathSum(TreeNode root, int sum) {
	if (root == null) {
        return false;
    }
    if (root.left == null && root.right == null) {
        return sum == root.val;
    }
	boolean left = hasPathSum(root.left, sum - root.val);
    boolean right = hasPathSum(root.right, sum - root.val);
    return left || right;
}

7.翻转二叉树(前序遍历)

public TreeNode invertTree(TreeNode root) {
	if (root == null) {
        return root;
	}
	TreeNode temp = root.left;
	root.left = root.right;
	root.right = temp;
	invertTree(root.left);
	invertTree(root.right);
	return root;
}

8.翻转二叉树(后序遍历)

public TreeNode invertTree(TreeNode root) {
	if (root == null) {
        return root;
	}
	TreeNode left = invertTree(root.left);
	TreeNode right = invertTree(root.right);
	root.left = right;
	root.right = left;
	return root;
}

9.翻转二叉树(层次遍历)

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return root;
        }
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            TreeNode left = temp.left;
            temp.left = temp.right;
            temp.right = left;
            if (temp.left != null) {
                queue.add(temp.left);
            }
            if (temp.right != null) {
                queue.add(temp.right);
            }
        }
        return root;
    }
}

如果对您有帮助,请点赞关注支持我,谢谢!❤
如有错误或者不足之处,敬请指正!❤
个人主页:星不易 ❤
算法通关村专栏:不易|算法通关村 ❤

你可能感兴趣的:(不易,算法通关村,算法,java,算法通关村)