110平衡二叉树
/**
* 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 isBalanced(TreeNode root) {
int result = f(root);
return result == -1?false:true;
}
int f(TreeNode root){
if(root == null) return 0;
int left = f(root.left);
int right = f(root.right);
if(Math.abs(left - right) > 1){
return -1;
}
if(left == -1 || right == -1){
return -1;
}
return Math.max(left,right) + 1;
}
}
257二叉树的所有路径
递归法:
回溯算法
/**
* 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 List<String> binaryTreePaths(TreeNode root) {
List<Integer> path = new ArrayList<>();
List<String> list = new ArrayList<>();
if(root == null) return list;
f(root,path,list);
return list;
}
void f(TreeNode root, List<Integer> path,List<String> list){
path.add(root.val);
if(root.left == null && root.right == null){
StringBuilder sb = new StringBuilder();
sb.append(path.get(0));
for(int i = 1;i < path.size();i++){
sb.append("->");
sb.append(path.get(i));
}
list.add(sb.toString());
return;
}
if(root.left != null){
f(root.left,path,list);
path.remove(path.size() - 1);
}
if(root.right != null){
f(root.right,path,list);
path.remove(path.size() - 1);
}
}
}
迭代法:
// 解法2
class Solution {
/**
* 迭代法
*/
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root == null)
return result;
Stack<Object> stack = new Stack<>();
// 节点和路径同时入栈
stack.push(root);
stack.push(root.val + "");
while (!stack.isEmpty()) {
// 节点和路径同时出栈
String path = (String) stack.pop();
TreeNode node = (TreeNode) stack.pop();
// 若找到叶子节点
if (node.left == null && node.right == null) {
result.add(path);
}
//右子节点不为空
if (node.right != null) {
stack.push(node.right);
stack.push(path + "->" + node.right.val);
}
//左子节点不为空
if (node.left != null) {
stack.push(node.left);
stack.push(path + "->" + node.left.val);
}
}
return result;
}
}
404左叶子之和
递归法:
/**
* 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 nums;
public int sumOfLeftLeaves(TreeNode root) {
f(root);
return nums;
}
//只算当前节点判断不出来是否为左节点,需要借助父节点
void f(TreeNode root){
if(root == null) return;
if(root.left != null && root.left.left == null && root.left.right == null){
nums += root.left.val;
}
f(root.left);
f(root.right);
}
}
迭代法:
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
Stack<TreeNode> stack = new Stack<> ();
stack.add(root);
int result = 0;
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.left != null && node.left.left == null && node.left.right == null) {
result += node.left.val;
}
if (node.right != null) stack.add(node.right);
if (node.left != null) stack.add(node.left);
}
return result;
}
}
层次遍历:
// 层序遍历迭代法
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int sum = 0;
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
while (size -- > 0) {
TreeNode node = queue.poll();
if (node.left != null) { // 左节点不为空
queue.offer(node.left);
if (node.left.left == null && node.left.right == null){ // 左叶子节点
sum += node.left.val;
}
}
if (node.right != null) queue.offer(node.right);
}
}
return sum;
}
}